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 this
or super
MUST be the first line of the constructor.
class FooClass {
int id;
String first;
String second;
String third;
FooClass(final int id, final String first) {
this(id, first, null);
}
FooClass(final int id, final String first, final String second) {
this(id, first, second, null);
}
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;
}
}
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 String
values.
Here is what calling them would look like:
//Example calls
public static void main(String [] args) {
FooClass foo1 = new FooClass(1, "foo1");
FooClass foo2 = new FooClass(2, "foo1", "foo2");
FooClass foo3 = new FooClass(3, "foo1", "foo2", "foo3");
}
You can't. As the error says, this
is available, but only if it is the first line (and therefore inherently you can only write one such call, total).
In theory you can do it, but whether it's clean - that's up for debate:
this(id, myStrings[0],
myStrings.length > 1 ? myString[1] : null,
myStrings.length > 2 ? myString[2] : null);
There is no general solution to it - you find a way to reduce the whole thing to a single this()
invocation, utilizing only static methods and basic expressions, or, you can't do it. In this specific case there was a way to get there with basic expressions, but that option isn't always available.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With