C
Christian Kreutzfeldt
Hi folks!
Tonight I ran over a quite interesting behavior I
would have not expected like that. It might be kind
of stupid, but I cannot explain this behavior to me
and hope that someone of you can explain it to me.
I have three classes A, B and C which are connected
as follows:
---------- Class A ----------
public abstract class ClassA {
private String var = null;
public ClassA(String var) {
this.var = var;
}
public String getVar() {
return var;
}
}
---------- Class B ----------
public abstract class ClassB extends ClassA {
public ClassB(String var) {
super(var);
abstractMethod();
}
protected abstract void abstractMethod();
}
---------- Class C ----------
public class ClassC extends ClassB {
private String instanceVar = "predefined-value";
public ClassC(String var) {
super(var);
System.out.println("#constructor: var: " +
getVar() + ", instanceVar: " + instanceVar);
}
protected void abstractMethod() {
instanceVar = "new-value";
System.out.println("#abstractMethod: var: " +
getVar() + ", instanceVar: " + instanceVar);
}
}
If I do create an instance of
ClassC (new ClassC("test")) the output looks like this:
#abstractMethod: var: test, instanceVar: new-value
#constructor: var: test, instanceVar: predefined-value
But my expectation concerning the output was:
#abstractMethod: var: test, instanceVar: new-value
#constructor: var: test, instanceVar: new-value
Why does the setting of the instance variable of
ClassC in ClassC#abstractMethod does not survive the
constructor call?
First I thought that the instance variable would be
initialized at the end of the constructor call which
runs through all parents. But in that case the call
of abstractMethod in the constructor of ClassB must
have crashed since its implementation in ClassC
accesses the instance variable instanceVar.
Kind regards,
Christian Kreutzfeldt
Tonight I ran over a quite interesting behavior I
would have not expected like that. It might be kind
of stupid, but I cannot explain this behavior to me
and hope that someone of you can explain it to me.
I have three classes A, B and C which are connected
as follows:
---------- Class A ----------
public abstract class ClassA {
private String var = null;
public ClassA(String var) {
this.var = var;
}
public String getVar() {
return var;
}
}
---------- Class B ----------
public abstract class ClassB extends ClassA {
public ClassB(String var) {
super(var);
abstractMethod();
}
protected abstract void abstractMethod();
}
---------- Class C ----------
public class ClassC extends ClassB {
private String instanceVar = "predefined-value";
public ClassC(String var) {
super(var);
System.out.println("#constructor: var: " +
getVar() + ", instanceVar: " + instanceVar);
}
protected void abstractMethod() {
instanceVar = "new-value";
System.out.println("#abstractMethod: var: " +
getVar() + ", instanceVar: " + instanceVar);
}
}
If I do create an instance of
ClassC (new ClassC("test")) the output looks like this:
#abstractMethod: var: test, instanceVar: new-value
#constructor: var: test, instanceVar: predefined-value
But my expectation concerning the output was:
#abstractMethod: var: test, instanceVar: new-value
#constructor: var: test, instanceVar: new-value
Why does the setting of the instance variable of
ClassC in ClassC#abstractMethod does not survive the
constructor call?
First I thought that the instance variable would be
initialized at the end of the constructor call which
runs through all parents. But in that case the call
of abstractMethod in the constructor of ClassB must
have crashed since its implementation in ClassC
accesses the instance variable instanceVar.
Kind regards,
Christian Kreutzfeldt