Order of Invokation of initializer blocks and constructor
- Parent static block
- Child static block
- Parent initialization block
- Parent Constructor
- Child initialization block
- Child Constructor
👆🏽 output of the code below
public class Parent {
public Parent() {
System.out.println("Parent Constructor");
}
static {
System.out.println("Parent static block");
}
{
System.out.println("Parent initialisation block");
}
}
public class Child extends Parent {
{
System.out.println("Child initialisation block");
}
static {
System.out.println("Child static block");
}
public Child() {
System.out.println("Child Constructor");
}
public static void main(String[] args) {
new Child();
}
}
Initialization blocks get invoked in the order they're written in the code.
References
- In what order do static blocks and initialization blocks execute when using inheritance? - Stack Overflow
- Order of execution of Initialization blocks and Constructors in Java
Backlinks