class DemoClass {
public static void main(String args[]) {
System.out.println("Start");
A a=new D();
}
}
class A {
static {
System.out.println("Static A");
A c=new C();
}
public A() {
System.out.println("Constr A");
}
}
class B extends A {
static {
System.out.println("Static B");
}
public B() {
System.out.println("Constr B");
}
}
class C extends B {
static {
System.out.println("Static C");
}
public C() {
System.out.println("Constr C");
}
}
class D extends C {
static {
System.out.println("Static D");
}
public D() {
System.out.println("Constr D");
}
}
The order of execution for above code is:
Start
Static A
Constr A
Constr B
Constr C
Static B
Static C
Static D
Constr A
Constr B
Constr C
Constr D
In my view all the static blocks should be executed first then only the object will be created. But, here the object "A c=new C()" in class A static block is created first and then the other static blocks are executed. Why?