Order of Initialization

Naveen AnamaneniUpdated · 5 min read

"What does this program print?" followed by a class full of static blocks, instance blocks and constructors is one of the most common Java interview puzzles. The answer isn't guesswork: Java follows one fixed order. Learn the two phases, class initialization (once) and object creation (every new), and you can predict any of these puzzles.

The Rule

Phase 1: Class initialization, once per class, the first time it's used.

  1. Parent class first, then child class.
  2. In each class: static field initializers and static blocks, top to bottom.

Phase 2: Object creation, every time new runs.

  1. Memory is allocated; every field gets its default value.
  2. Parent part first: the parent's field initializers and instance blocks (top to bottom), then the parent's constructor body.
  3. Then the child: its field initializers and instance blocks (top to bottom), then the child's constructor body.

Watch Every Step

Static parts once, instance parts for every object
Order.java▶ Try it
public class Order {
    static class Parent {
        static { System.out.println("1. Parent static block"); }
        { System.out.println("3. Parent instance block"); }
        Parent() { System.out.println("4. Parent constructor"); }
    }
 
    static class Child extends Parent {
        static { System.out.println("2. Child static block"); }
        int x = log("5. Child field initializer");
        { System.out.println("6. Child instance block"); }
        Child() { System.out.println("7. Child constructor"); }
 
        static int log(String s) { System.out.println(s); return 1; }
    }
 
    public static void main(String[] args) {
        System.out.println("-- first object --");
        new Child();
        System.out.println("-- second object --");
        new Child();
    }
}
Output
-- first object --
1. Parent static block
2. Child static block
3. Parent instance block
4. Parent constructor
5. Child field initializer
6. Child instance block
7. Child constructor
-- second object --
3. Parent instance block
4. Parent constructor
5. Child field initializer
6. Child instance block
7. Child constructor

How it works

  1. The first new Child() triggers class initialization: Parent's static block (1), then Child's (2).
  2. Then the object is built, parent part first (3, 4), then child part: the field initializer and instance block in the order written (5, 6), then the constructor body (7).
  3. The second new Child() skips the static blocks; the classes are already initialized. Only 3 to 7 repeat.

extends makes Child a subclass of Parent (see "Inheritance in Java").

Try it yourself: move line 11 (the child's instance block) above line 10 (the field). Which two lines swap in the output?

Common Mistakes

Thinking field initializers run before the parent constructor. The parent's whole construction finishes first.

Expecting static blocks to run for every object. Only once per class.

Ignoring textual order. A field initializer below an instance block runs after it.

Under the Hood

Run javap -c on Child and you'll see that the compiler copied the field initializer and the instance block into the constructor, right after the call to the parent's constructor:

javap output
Child();
  Code:
     0: aload_0
     1: invokespecial #1   // Method Parent."<init>":()V
     4: aload_0
     5: ldc           #7   // String 5. Child field initializer
     7: invokestatic  #9   // Method log:(Ljava/lang/String;)I
    10: putfield      #15  // Field x:I
    13: getstatic     #19  // Field java/lang/System.out
    16: ldc           #25  // String 6. Child instance block
    ...
    24: ldc           #33  // String 7. Child constructor

In bytecode, constructors are named <init>, and all static initializers of a class are merged into one method called <clinit> (class init), which the JVM runs once. That's the whole explanation of the order.

Interview Corner

What is the order of execution of static blocks, instance blocks and constructors?

When a class is first used, its static initializers and static blocks run once, top to bottom, with parent classes before child classes. For each new object, the parent's instance initializers and constructor run first, then the child's field initializers and instance blocks in textual order, then the child's constructor body.

What are init and clinit?

They're the bytecode names of initialization methods. <init> is a constructor, into which the compiler copies field initializers and instance blocks. <clinit> is the class initializer, containing all static field initializers and static blocks, run once by the JVM when the class is initialized.

Practice

  1. Easy: Without running it, write down what the program prints if main creates only one Parent object.
  2. Harder: Add a static field static int count = log("0. Child static field"); above Child's static block. Predict the new first lines, then check.

Quick check: Initialization order

4 questions
1Which runs first?
2How many times do static blocks run when three objects are created?
3When does a child's instance block run?
4What is <clinit> in bytecode?

Key Takeaways

Was this tutorial helpful?

Lesson 109 of 482