is it a good practice that having overloading constructors that some of it doesnt have anything on it?
ill post a code herepublic 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..