# Object Initialization Order in Java — Static Blocks, Instance Blocks and Constructor Chaining

Source: https://skills101.in/learn/oops/oop-object-initialization-order — Skills101 free tutorial (OOP Concepts). Updated 2026-11-21.

Placement tests often show a short Java program with a few `static { }` blocks, some `{ }` blocks and constructors spread across a parent and a child class, and ask: **"What is the output?"** You can answer these with certainty once you know the rules. This lesson builds them up one at a time, with a program for each rule.

## 1. Static blocks run once, when the class is first used

A **static block** (`static { ... }`) belongs to the class, not to any object. Java runs it **once**, when the class is **initialised** — that is, the first time the program actually uses the class (creates an object, calls a static method, or reads a static field that is not a constant).

```java
class College {
    static int count;

    static {
        System.out.println("static block: College is ready");
        count = 0;
    }

    College() {
        count++;
        System.out.println("constructor: count = " + count);
    }
}

public class Main {
    public static void main(String[] args) {
        System.out.println("main starts");
        new College();
        new College();
    }
}
```

```text
main starts
static block: College is ready
constructor: count = 1
constructor: count = 2
```

Two things to notice. The static block ran **after** `main starts`, because `College` was not used until the first `new College()`. And it ran **only once**, even though two objects were created. Static blocks are used to set up static data, such as loading a lookup table.

## 2. Inside one object: fields and instance blocks, then the constructor body

An **instance initialiser block** is a plain `{ ... }` inside the class. Java copies field initialisers (like `String name = ...;`) and instance blocks into **every** constructor, in the order they appear in the source, right after the call to the parent constructor. So they run **before the constructor body**.

```java
class Student {
    String name = log("field initialiser");

    {
        log("instance block");
    }

    Student() {
        log("constructor body");
    }

    static String log(String msg) {
        System.out.println(msg);
        return msg;
    }
}

public class Main {
    public static void main(String[] args) {
        new Student();
    }
}
```

```text
field initialiser
instance block
constructor body
```

If the instance block were written **above** the field, it would run first. The rule is "top to bottom in the source", not "fields before blocks".

## 3. With inheritance: parent before child

Every constructor starts by calling a parent constructor — either one you write as `super(...)`, or an invisible `super()` that Java adds for you. That gives the full order:

1. **Static** parts of the parent, then of the child — only the first time the classes are used.
2. For each new object: the **parent's** instance part (fields, instance blocks, constructor body).
3. Then the **child's** instance part.

```java
class Person {
    static { System.out.println("Person static block"); }
    { System.out.println("Person instance block"); }
    Person() { System.out.println("Person constructor"); }
}

class Student extends Person {
    static { System.out.println("Student static block"); }
    { System.out.println("Student instance block"); }
    Student() { System.out.println("Student constructor"); }
}

public class Main {
    public static void main(String[] args) {
        System.out.println("--- first object ---");
        new Student();
        System.out.println("--- second object ---");
        new Student();
    }
}
```

```text
--- first object ---
Person static block
Student static block
Person instance block
Person constructor
Student instance block
Student constructor
--- second object ---
Person instance block
Person constructor
Student instance block
Student constructor
```

For the second object there are no static lines: classes are initialised only once per program run.

![Numbered steps for new Student(): Person static block, Student static block (both once), then Person instance block, Person constructor, Student instance block, Student constructor (every new)](https://skills101.in/learn-img/oops/init-order.svg)

A way to remember it: **a child cannot be built before its parent part exists**. A `Student` object contains a `Person` part, so that part is completed first.

## 4. Constructor chaining with this() and super()

A constructor can call another constructor of the **same** class with `this(...)`, or a constructor of the **parent** with `super(...)`. This is called **constructor chaining**. It avoids repeating the same assignments in several constructors.

```java
class Ticket {
    String from, to;
    int seats;

    Ticket(String from, String to) {
        this(from, to, 1);                                   // default: 1 seat
        System.out.println("2-argument constructor done");
    }

    Ticket(String from, String to, int seats) {
        this.from = from;
        this.to = to;
        this.seats = seats;
        System.out.println("3-argument constructor: " + seats + " seat(s) " + from + " -> " + to);
    }
}

public class Main {
    public static void main(String[] args) {
        new Ticket("Delhi", "Jaipur");
    }
}
```

```text
3-argument constructor: 1 seat(s) Delhi -> Jaipur
2-argument constructor done
```

The rules:

| Rule | Reason |
|---|---|
| `this(...)` or `super(...)` must be the **first statement** (the classic rule) | The object must be set up before your code uses it |
| A constructor cannot have both `this(...)` and `super(...)` | Only one of them can be first; the `this(...)` target will call `super` itself |
| If you write neither, Java inserts `super()` | So the parent **must** have a no-argument constructor, or you get a compile error |
| Instance blocks run once per object, even with `this(...)` | They are copied only into constructors that call `super`, not `this` |

Java 25 relaxed the first rule a little: it allows statements before `this(...)`/`super(...)` as long as they do not use the object being built (for example, checking an argument). College syllabi and placement tests still assume the classic rule.

The third rule causes a common compile error: if `Person` has only `Person(String name)`, then `class Student extends Person { Student() { } }` does not compile, because the hidden `super()` has nothing to call. The fix is `super("some name");` as the first line.

## 5. The trap: an overridable method called from a constructor

What happens if the parent constructor calls a method that the child overrides?

```java
class Base {
    Base() { describe(); }
    void describe() { System.out.println("Base"); }
}

class Train extends Base {
    String name = "Rajdhani";
    int coaches = 16;

    Train() { describe(); }

    @Override
    void describe() { System.out.println(name + " with " + coaches + " coaches"); }
}

public class Main {
    public static void main(String[] args) {
        new Train();
    }
}
```

```text
null with 0 coaches
Rajdhani with 16 coaches
```

The first line is `null with 0 coaches`. Here is why:

1. `new Train()` starts. Memory for all fields is set to **default values**: `null` for objects, `0` for numbers.
2. `Train()` first calls `Base()`.
3. `Base()` calls `describe()`. Because of runtime polymorphism, **`Train`'s** `describe()` runs — but `Train`'s field initialisers have **not run yet**.
4. `Base()` finishes; now `name` and `coaches` get their values; then `Train()`'s body calls `describe()` again, which prints the real values.

![The constructor trap: Base() calls the overridden describe() before Train's fields are set, so it sees null and 0; later Train() calls it again and sees Rajdhani and 16](https://skills101.in/learn-img/oops/constructor-trap.svg)

The advice that follows: **do not call overridable methods from a constructor.** Call only `private`, `static` or `final` methods there, which subclasses cannot override.

## Common mistakes

- Thinking static blocks run when the program starts. They run when the class is first **used**.
- Expecting static blocks to run again for every object.
- Assuming fields are always set before instance blocks. Order is simply top to bottom in the source.
- Putting a statement before `this(...)` or `super(...)` — a compile error in Java versions before 25, which most courses and tests still use.
- Removing a parent's no-argument constructor and getting "constructor Person in class Person cannot be applied" errors in every subclass.
- Calling an overridable method from a constructor and reading half-initialised fields.

## Quick recap

- Static blocks: once per class, at first use, parent before child.
- Per object: parent's fields/instance blocks/constructor body, then the child's.
- Field initialisers and instance blocks run before the constructor body, top to bottom.
- `this(...)` or `super(...)` must come first; if absent, Java adds `super()`.
- Never call an overridable method from a constructor — the child's fields still hold default values.


Practice quiz and free certificate: https://skills101.in/learn/oops/oop-object-initialization-order