Object Initialisation Order
The exact order of static blocks, field initialisers, instance blocks and constructors.
Open this lesson in the learning hubKey points
- Static blocks and static fields run once, when the class is first loaded, parent before child.
- Then per object: the parent chain runs fully, then the child field initialisers and instance blocks in text order.
- The child constructor body is always last. That is why a parent constructor sees child fields still unset.
- A field with no initialiser is already zeroed:
0,false, ornull. Never garbage. - Static blocks are for expensive one-time setup. Anything that can throw there gives you an ExceptionInInitializerError.
- Instance blocks are rare in real code. Prefer putting the work in the constructor where readers expect it.
Example
public class Main {
static class Base {
static { System.out.println("1. Base static block (once, at class load)"); }
{ System.out.println("3. Base instance block"); }
Base() { System.out.println("4. Base constructor"); }
}
static class Child extends Base {
static { System.out.println("2. Child static block (once, at class load)"); }
private final String field = trace("5. Child field initialiser");
{ System.out.println("6. Child instance block"); }
Child() { System.out.println("7. Child constructor -> " + field); }
static String trace(String s) { System.out.println(s); return "ready"; }
}
public static void main(String[] args) {
System.out.println("--- first new Child() ---");
new Child();
System.out.println("--- second new Child() ---");
new Child(); // static blocks do NOT run again
}
}
Statics load once; per object the parent finishes before the child body starts.
This is a reading copy. The full lesson — with the visual explainer, the interactive lab and a Run button for the code — lives in the OOP course, and every lesson in it is listed on the OOP contents page.