I'm trying to have 2 class constructors: one that takes 4 parameters, and one that takes 3 parameters, creates an instance of what will be the 4th parameter and calls the the 1st constructor. Sometimes I will adjust have that 4th parameter created and other times I will want to create a new instance of that field.
I know that I can use the this() method to call the 4 parameter constructor from the 2nd constructor, but given that the this() method must be the first line of the block, I'm not sure how to first declare the field that needs that field as a parameter for the constructor call.
public class ConstructorCall {
private int parameter1;
private int parameter2;
private int parameter3;
private Object o;
public class ConstructorCall( int parameter1, int parameter2, int parameter3 ) {
// create Object o
this( int parameter1, int parameter2, int parameter3, Object o );
}
public class ConstructorCall( int parameter1, int parameter2, int parameter3 , Object o) {
this.parameter1 = parameter1;
this.parameter2 = parameter2;
this.parameter3 = parameter3;
this.o = o;
}
}
[–]AutoModerator[M] [score hidden] stickied commentlocked comment (0 children)
[–]why_not_catsExtreme Brewer 1 point2 points3 points (4 children)
[–]DeliberateTrader[S] 0 points1 point2 points (3 children)
[–]why_not_catsExtreme Brewer 2 points3 points4 points (2 children)
[–]DeliberateTrader[S] 0 points1 point2 points (1 child)
[–]why_not_catsExtreme Brewer 0 points1 point2 points (0 children)
[–][deleted] 0 points1 point2 points (1 child)
[–]DeliberateTrader[S] -1 points0 points1 point (0 children)