Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Overloading constructors(Multiple Constructors)

  1. #1
    Java kindergarten chronoz13's Avatar
    Join Date
    Mar 2009
    Location
    Philippines
    Posts
    659
    Thanks
    177
    Thanked 30 Times in 28 Posts

    Default Overloading constructors(Multiple Constructors)

    is it a good practice that having overloading constructors that some of it doesnt have anything on it?

    ill post a code here
    public final class RandomWords {
     
        private ArrayList wordList;
        private String word;
        private String tempDatabaseWords[] = {"Zabdiel", "waha"};
     
        public int wordListSize; 
     
        public RandomWords(int size) {
     
            wordList = new ArrayList();
            wordListSize = size;
            generateRandomWordList();
        }
     
        public RandomWords() {
     
     
        }
     
        public void generateRandomWordList() {
     
            for (int x = 0; x < wordListSize; x++) {
     
                int randomWord = (int) Math.round(Math.random() * (tempDatabaseWords.length - 1));
                word = tempDatabaseWords[randomWord];
                wordList.add(word);
            }
        }
     
        public String generateRandomWord() {
     
            int randomWord = (int) Math.round(Math.random() * (tempDatabaseWords.length - 1));
            return word = tempDatabaseWords[randomWord];
        }
     
        public ArrayList getRandomWordList() {
     
            return wordList;
        }

    as you can see i have 2 constructors, one has something on it that initializes some data members and 1 that doesnt have anything, the problem is if i only have the first constructor, and i need only a simple object initialization or a simple task i am oblige to use the first constructor, well in some case i dont need it, to make it clear.. for example

    there are 2 methods in the class.. one that creates a LISTS of randomwords and another 1 that generates a single randomword.. the first constructor initializes an arraylist... that is going to use for a list of randomly generated Strings,
    if i only need to call the method generateRandomWord (a single word) only, and i dont need the arrayList object(wordList). i need to initialize the object using the first constructor..

    my concern is.. is it a good practice leaving another constructor that doesnt have anything. like... using it only for initializing an object?

    i have a way to accomplish my goal... like creating a different methods to initializes the data members and leaving the class having 1 constructor... but im curious about this one..
    Last edited by chronoz13; May 11th, 2011 at 02:13 AM.


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Overloading constructors(Multiple Constructors)

    In my opinion code you posted is not good practice, mainly since if one uses the empty parameter constructor and tries to get the word List, a NullPointerException would be thrown when one tries to access that list. In other words it seems you are trying to stuff two behaviors into a single class (Single word vs Multiple Words), and relying on the caller to know which constructor to use for each behavior. There are many ways to adapt the code, for example 1) In the empty parameter constructor, call this(n) , where n is a specified default size - this tries to boil the 2 behaviors into one - a random word class 2) Create 2 classes, one for each behavior: the first generates a Single random word, the other uses the first class to generate a List of random words - 2 behaviors, 2 classes.

  3. The Following User Says Thank You to copeg For This Useful Post:

    chronoz13 (May 11th, 2011)

  4. #3
    Java kindergarten chronoz13's Avatar
    Join Date
    Mar 2009
    Location
    Philippines
    Posts
    659
    Thanks
    177
    Thanked 30 Times in 28 Posts

    Default Re: Overloading constructors(Multiple Constructors)

    stuff two behaviors into a single class
    oh yes... i forgot.. the task of the class is for generating RandomWords with "S" .. i should have created another class for another task for a different behavior,
    In my opinion code you posted is not good practice,
    well you answered my question very clear .. thanks again mate ..

Similar Threads

  1. Write methods and constructors from Javadocs
    By smashX in forum What's Wrong With My Code?
    Replies: 3
    Last Post: April 20th, 2011, 10:23 PM
  2. Method Overloading - Doubt
    By vidya lakshman in forum Java Theory & Questions
    Replies: 2
    Last Post: January 31st, 2011, 09:32 AM
  3. Overloading vs Overriding
    By nickypass in forum Java Theory & Questions
    Replies: 1
    Last Post: October 17th, 2010, 01:53 AM
  4. Array object and constructors
    By TarunN in forum Collections and Generics
    Replies: 14
    Last Post: May 6th, 2010, 04:04 PM
  5. Constructors, Hash Tables, & Linked Lists
    By illusion887 in forum Collections and Generics
    Replies: 2
    Last Post: December 3rd, 2009, 03:46 AM