Core Java: Instance initializer blocks run before the constructor body

Field initializers and instance initializer blocks run in textual order as the object is built, and all of them finish before the constructor's own body executes.

Code
class Widget {
    int a = log("field a");
    {
        log("instance initializer");
    }
    int b = log("field b");
    Widget() {
        log("constructor");
    }
    static int log(String msg) {
        System.out.println("Running: " + msg);
        return 0;
    }
}
new Widget();
Output
Running: field a
Running: instance initializer
Running: field b
Running: constructor
Advertisement
More in JAVA

Run this yourself in the Online Java Compiler, spin up a live REST API in the API Sandbox, or practise with Java interview questions.

Published 2026-09-27

© Java Coding Hub · About · Contact · Privacy · Terms