Java Programming Tutorials

Java programming tutorials with many code examples!

Java Initialization order

Java Initialization order

In this tutorial we’re going to show what is initialization order of fields (instance and static) in Java when JVM loads a class.

Ordering rules

  1. Static fields and static blocks of ancestors.
    In each class they are initialized in order of appearance.
  2. Static fields and static block of instantiated class.
  3. Instance fields and initialization blocks of ancestors.
    In each class they are initialized in order of appearance.
  4. Constructor of ancestor after initialization of its instance fields.
  5. Instance fields and initialization blocks of the current class.
    Also in order of appearance.
  6. Constructor of the current class.

Initialization example:

package com.farenda.java.lang;

public class InitializationOrder {

    // Prints message and returns given object
    static <T> T init(String name, T object) {
        System.out.printf("Initializing '%s' to '%s'%n",
                name, object);
        return object;
    }

    static class Base {
        private int i = init("Base instance i", 5);

        private static int x = init("Base static x", 1);

        private int ctorI;

        public Base() {
            ctorI = init("Base ctorI", 7);
        }

        private static int y = init("Base static y", 2);

        { // instance initialization block:
            j = init("Base instance block j", 6);
        }

        private static int z;
        static { // static initialization block:
            z = init("Base.z static block", 3);
        }

        private int j;
    }

    static class Subclass extends Base {

        public Subclass() {
            ctorB = init("Subclass ctorB", 9);
        }

        int ctorB;

        int a = init("Subclass instance a", 8);

        static int foo = init("Subclass static foo", 4);
    }

    public static void main(String[] args) {
        System.out.println("First Subclass instance:");
        Base base = new Subclass();

        System.out.println("Another Subclass instance:");
        base = new Subclass();

        System.out.println("Instance of Base:");
        base = new Base();
    }
}

The above code clearly shows initialization order:

First Subclass instance:
Initializing 'Base static x' to '1'
Initializing 'Base static y' to '2'
Initializing 'Base.z static block' to '3'
Initializing 'Subclass static foo' to '4'
Initializing 'Base instance i' to '5'
Initializing 'Base instance block j' to '6'
Initializing 'Base ctorI' to '7'
Initializing 'Subclass instance a' to '8'
Initializing 'Subclass ctorB' to '9'

Another Subclass instance:
Initializing 'Base instance i' to '5'
Initializing 'Base instance block j' to '6'
Initializing 'Base ctorI' to '7'
Initializing 'Subclass instance a' to '8'
Initializing 'Subclass ctorB' to '9'

Instance of Base:
Initializing 'Base instance i' to '5'
Initializing 'Base instance block j' to '6'
Initializing 'Base ctorI' to '7'
Share with the World!