I have a simple class with a constructor of all available fields and i want to add an overloaded constructor with varargs to allow the creating of objects even if some fields are unknown.
class FooClass {
int id;
String first;
String second;
String third;
FooClass(final int id, final String first, final String second, final String third) {
this.id = id;
this.first = first;
this.second = second;
this.third = third;
}
FooClass(final int id, final String... myStrings){
if(myStrings.length == 1) {
this.id = id;
this.first = myStrings[0];
} else if (myStrings.length == 2) {
this.id = id;
this.first = myStrings[0];
this.second = myStrings[1];
} else {
this.id = id;
this.first = myStrings[0];
this.second = myStrings[1];
this.third = myStrings[2];
}
}
}
I wanted to refactor the second constructor to avoid repeating this.id, this.first ... as follows, but I am getting a compile error
FooClass(final int id, final String... myStrings){
if(myStrings.length == 1) {
this(id, myStrings[0], null, null);
} else if (myStrings.length == 2) {
this(id, myStrings[0], myStrings[1], null);
} else {
this(id, myStrings[0], myStrings[1], myStrings[2]);
}
}
The error is:
Call to 'this()' must be first statement in constructor body
What is the correct way to call the base constructor from the varargs constructor with null values? Or a better way to implement what I described above?
In Java, a call to
thisorsuperMUST be the first line of the constructor.One approach to get similar functionality to varargs is to simply write multiple constructors that chain upwards with default values passed.
This could get cumbersome though if the number of parameters is high, but the example is only using 3
Stringvalues.Here is what calling them would look like: