The below has this output.
Hello World!
main.ConstructedDerivedClass:6.0
main.ConstructedDerivedClass:6.0
public class ConstructedDerivedClass extends ConstructedBase {
private static final double version = 6.0;
public static void main(String[] args) {
System.out.println("Hello World!");
ConstructedDerivedClass derivedClass = new ConstructedDerivedClass();
}
public ConstructedDerivedClass() {
showMyAttributes();
}
@Override
protected void showMyAttributes() {
System.out.println(this.getClass().getName() + ":" + version);
}
}
public class ConstructedBase {
private static final double version = 15.0;
public ConstructedBase() {
showMyAttributes();
}
protected void showMyAttributes() {
System.out.println(this.getClass().getName() + ":" + version);
}
}
I would expect it to just display one line, that of the child class (ConstructedDerivedClass). But instead it print's out twice.I know in general you should avoid calling overriden methods from a constructor, but I wanted to see for myself how was this working.
Actually, I get why version is '6.0' on both lines - since field is being declared static of course static fields are initialized first. But still don't get why two lines.
Any guidance would be appreciated.