public class Sentencegenerator {
private String[] subjects;
private String[] verbs;
private String[] directObjects;
public Sentencegenerator(){
subjects = {"Cat", "Dog", "Joe", "Teacher", "Policeman", "Doctor", "Dick"};
verbs = {"eats", "reads", "cums", "farts", "poops", "stabs", "cries"};
directObjects = {"book", "sticker", "fish", "man", "chiwawa", "marker", "cheese"};
}
public Sentencegenerator(String[] mySubjects, String[] myVerbs, String[] myDirectObjects){
subjects = mySubjects;
verbs = myVerbs;
directObjects = myDirectObjects;
}
I know you can only create array constants when you initialize an array, but is there a way I can make an array instance variable and then make a constructor that has its own constants?
There are two ways to instantiate an array to a constant array:
String[] subjects = {"Cat", "Dog", "Joe", "Teacher", "Policeman", "Doctor", "Dick"};
or:
String[] subjects;
subjects = new String[] {"Cat", "Dog", "Joe", "Teacher", "Policeman", "Doctor", "Dick"};
In your constructor, you need to use the latter (note the new String[]
part).