I have the following class:
class Pair
{
String car;
Integer cdr;
public Pair () {}
public Pair (String car) { this.car = car; }
public Pair (Integer cdr) { this.cdr = cdr; }
public Pair (String car, Integer cdr)
{
this(car);
this(cdr);
}
}
The class contains two optional values and I would like to provide all possible constructor permutations. The first version does not initialize anything, the second initializes only the first value and the third initializes only the second value.
The last constructor is the combination of the second and third one. But it is not possible to write this down, because the code fails with.
constructor.java:13: call to this must be first statement in constructor this(cdr); ^ 1 error
Is it possible to write the last constructor without any code redundancy (also without calling the same setter methods)?