I know that static blocks are initialized during class loading, and since the class is loaded only once in the program, they are initialized only once.
IIB (instance initialization blocks) are initialized every time an instance of the class is created, and the same for constructors: they are executed during the creation of the object.
I do not understand why in the below program IIB is executed before the constructors. The code -
public class Hello {
public static void main(String args[]) {
C obj = new C();
}
}
class A {
static {
System.out.println("Inside static block of A");
}
{
System.out.println("Inside IIB of A");
}
A() {
System.out.println("Inside NO-ARGS constructor of A");
}
}
class B extends A {
static {
System.out.println("Inside static block of B");
}
{
System.out.println("Inside IIB of B");
}
B() {
System.out.println("Inside NO-ARGS constructor of B");
}
}
class C extends B {
static {
System.out.println("Inside static block of C");
}
{
System.out.println("Inside IIB of C");
}
C() {
System.out.println("Inside NO-ARGS constructor of C");
}
}
Why is IIB executed first compared to designers?
source
share