Order of Invokation of initializer blocks and constructor

  1. Parent static block
  2. Child static block
  3. Parent initialization block
  4. Parent Constructor
  5. Child initialization block
  6. 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


Backlinks