I read that instance variables of the child class get initialized after constructor of the super class. So why initialize()
in class B
prints 0, but not an error that symbol f1
not found? And why it prints 0, but not 3?
public class Test {
public static void main(String[] args) {
new B();
}
public static class A {
public static int f1 = 7;
public A() {
initialize(); // I know that should not use method in the constructor
}
protected void initialize() {
System.out.println(f1);
}
}
public static class B extends A {
public int f1 = 3;
public B() {}
protected void initialize() {
System.out.println(f1); // why 0 but not an error: can not resolve symbol f1
}
}
}