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
Code :
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..
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.
Re: Overloading constructors(Multiple Constructors)
Quote:
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,
Quote:
In my opinion code you posted is not good practice,
well you answered my question very clear .. thanks again mate ..