Suppose I have three constructors in a class:
public class MyClass {
Parent thingA;
Child thingB;
boolean someBoolean;
double someDouble;
double anotherDouble;
public MyClass(Parent thingA, boolean someBoolean) {
this(thingA, someBoolean, 0.25);
}
public MyClass(Child thingB, boolean someBoolean, double anotherDouble) {
// Casting to prevent recursive constructor call
this((Parent) thingB, someBoolean, 0.5);
this.thingB = thingB;
this.anotherDouble = anotherDouble;
}
public MyClass(Parent thingA, boolean someBoolean, double someDouble) {
this.thingA = thingA;
this.someBoolean = someBoolean;
this.someDouble = someDouble;
}
}
Where Child
extends Parent
. My question is, when I call another constructor from a constructor in Java via this(...)
, am I passing by reference or passing by value? How does casting affect it? If I were to use the second constructor and modify a property of thingB, would that reflect in thingA?
Edit: I have both thingA and thingB fields because sometimes my class will be used without an instance of Child
, but when Child
is used, I need to take advantage of specific behaviors of Child
.