I want to know that does default initialization of static variables happens before static initialization or along with it ?
Let us look at code below:
class Test {
static {
System.out.println(Test.a); // prints 0
a = 99; // a get 99
System.out.println(Test.a); // prints 99
}
static int a = 10;
static {
System.out.println(a); // prints 10
}
public static void main(String[] args) {
}
}
My guess is that when class is loaded , all the static variables are created in memory and default values given to them then and there only. Then static initialization takes place in the order static initializers and static fields that appear in code . Am I right ?
EDIT : I am aware of the order of static initialization but I am unable to get the point in JLS where it tells us that default initialization happens before static initialization takes place ?