100% Free Forever
AI-Powered Learning
Industry Expert Content
Certificates & Badges
Learn At Your Own Pace

What is an Instance Initializer Block?

Learn what an instance initializer block is in Java, when it runs relative to constructors, and how it differs from static blocks.

mediumQ211 of 226 in Object Oriented Programming Est. time: 5 minsLast updated:
Open Code Lab

Expected Interview Answer

An instance initializer block is a plain `{ ... }` block (no `static` keyword) written directly in a Java class body that runs once every time a new object is created, immediately after the superclass constructor completes and before the class’s own constructor body executes.

Unlike a static initializer block, which runs once per class, an instance initializer block runs once per object, every single time `new` is called — if there are five constructors, an instance block still runs before every one of them, unless that constructor chains via `this(...)` to another constructor (in which case it only runs once, at the actual chain target). This makes it useful for setup logic shared across multiple overloaded constructors, avoiding duplicated code in each one, or for initializing anonymous class instance state where no named constructor can be written at all. Execution order within a class is: field initializers and instance blocks run in source order first, then the constructor body runs last. They are less commonly used than static blocks in everyday Java code, but they are the mechanism anonymous classes rely on for any setup beyond a single expression.

  • Shares setup logic across multiple overloaded constructors without duplication
  • Runs before the constructor body, after field initializers, in a predictable order
  • Only mechanism available for multi-statement setup in anonymous classes
  • Keeps common initialization out of every individual constructor

AI Mentor Explanation

Every player, no matter which specific pre-match routine (opener’s routine, bowler’s routine, fielder’s routine) they follow, first goes through the same mandatory kit check before their specialized routine begins — that shared kit check happens fresh, every single time, for every single player who takes the field. An instance initializer block is this shared kit check: it runs once per object created, before whichever specific constructor runs, ensuring common setup happens no matter which constructor path was used.

Step-by-Step Explanation

  1. Step 1

    Object creation begins

    `new ClassName(...)` is called, triggering the superclass constructor first.

  2. Step 2

    Field initializers and instance blocks run in source order

    Any `{ ... }` instance blocks execute interleaved with field declarations, top to bottom.

  3. Step 3

    Runs before every constructor, unless chained

    It executes before the body of whichever constructor is invoked, except when that constructor delegates via `this(...)`.

  4. Step 4

    Constructor body runs last

    After field initializers and instance blocks complete, the constructor’s own code executes.

What Interviewer Expects

  • Correct trigger: runs once per object, before the constructor body, after field initializers
  • Understanding it runs for every constructor except one reached via `this(...)` chaining
  • A realistic use case: shared setup across multiple constructor overloads, or anonymous class setup
  • Clear distinction from static initializer blocks (once per class vs once per object)

Common Mistakes

  • Confusing instance initializer blocks with static initializer blocks
  • Assuming it only runs for one specific constructor instead of all non-chained ones
  • Forgetting it runs before the constructor body, not after
  • Not recognizing it as the mechanism anonymous classes use for multi-statement setup

Best Answer (HR Friendly)

An instance initializer block is a plain block of code in a Java class, without the static keyword, that runs every single time a new object is created — right after the parent class’s constructor finishes but before that class’s own constructor body runs. It’s handy for sharing setup logic across several constructors so you don’t repeat it in each one, and it’s also the way anonymous classes handle setup that needs more than one line.

Code Example

Instance initializer block shared across constructors
class Session {
    private final String id;
    private final long createdAt;

    // Instance initializer block: runs before every constructor below
    {
        createdAt = System.currentTimeMillis();
        System.out.println("Session setup running for new instance");
    }

    Session() {
        this.id = "anonymous";
    }

    Session(String id) {
        this.id = id;
    }
}

Session s1 = new Session();      // prints setup message, id = "anonymous"
Session s2 = new Session("s42"); // prints setup message again, id = "s42"

Follow-up Questions

  • What is the execution order between field initializers, instance blocks, and the constructor body?
  • Does an instance initializer block run if a constructor delegates via `this(...)`?
  • Why do anonymous classes rely on instance initializer blocks instead of constructors?
  • How does an instance initializer block differ from a static initializer block?

MCQ Practice

1. When does an instance initializer block execute relative to a constructor?

Instance blocks run after the superclass constructor completes but before the current class’s constructor body executes.

2. How many times does an instance initializer block run if a class has three constructors and five objects are created (none using `this(...)` chaining)?

The block runs once per object creation, so five objects trigger it five times, regardless of which constructor overload each used.

3. What is a key legitimate use case for instance initializer blocks?

They are used to share initialization logic across overloaded constructors, and are the only way to add multi-statement setup to anonymous classes.

Flash Cards

Instance initializer block in one line?A `{ }` block that runs once per object, before the constructor body.

Runs how many times?Once per object created — for every constructor, unless reached via `this(...)` chaining.

Key use case?Sharing setup logic across multiple constructors, or setup in anonymous classes.

Differs from static initializer block how?Static blocks run once per class load; instance blocks run once per object.

1 / 4

Continue Learning