Understanding about loading a class and calling static initializer
In what order do static initializer blocks
So, I just tried to confirm it -
public class OOMErrorB extends OOMErrorA {
public static int c = 10;
static {
System.out.println("Loading static B " + c);
System.out.println(OOMErrorA.a);
}
public static void main(String[] args) {
new OOMErrorB();
}
}
The parent class is -
public class OOMErrorA {
public static int a = 20;
static {
a = a+ OOMErrorB.c;
System.out.println("OOMErrorB.c " + OOMErrorB.c);
System.out.println("loading OOMErrorA.a " + a);
}
}
Now Output of main method of B -
**
OOMErrorB.c 0
loading OOMErrorA.a 20
Loading static B 10
20
**
I can understand that first it is loading class A because it's Super class and calling it's static initializers,
now since i am accessing OOMErrorB.c in static block of OOMErrorA , it should load and call static initializer of OOMErrorB. so, OOMErrorB.c should be 10 and not 0 .
What i know about loading and initializing of a class -
1) Class and gets loaded and variables are initialized to default values like for int - 0, Object - null.
2) Class field are initialized to specified values.
3) Static block gets called .
here in my program , i can see that class OOMErrorB got loaded (step 1) , but step 2 and step 3 didn't execute.
whereas according to accepted answer on link, it should call static initializer of OOMErrorB.
so it should end up in a cyclic dependency ?