Java Reference ยท Object-Oriented Concepts
Java initialization order
The exact order Java initializes classes and objects: static blocks, field initializers, instance blocks, constructors and superclasses, with traces to run.
Java initializes things in a fixed, predictable order. Knowing it explains bugs where a field is unexpectedly null or 0, where a static block seems to run "too early", or where an overridden method sees uninitialized state.
There are two separate phases. Class initialization happens once per class, the first time the class is actively used (an instance is created, a static method is called, or a non-constant static field is accessed). Instance initialization happens every time new creates an object.
The order is:
- Class initialization: superclass first, then static field initializers and
static { }blocks of the class in textual order. - Instance creation: memory is allocated and all fields get default values (
0,false,null). - The constructor starts and immediately calls the superclass constructor (explicitly or implicitly).
- After the
super(...)call returns, instance field initializers and{ }instance blocks run in textual order. - The rest of the constructor body runs.
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.
Rules in detail
| Step | What happens |
|---|---|
| Class loading and linking | Done before first use; initialization is separate and lazy. |
| Static initialization | Runs once, thread-safely (the JVM holds a class-init lock), in textual order of static fields and static blocks. |
| Field defaults | Every instance field is zeroed before any code runs on the new object. |
super() |
Runs the whole chain up to Object first, so parents are fully built before children. |
| Instance initializers | Field initializers and instance blocks, in the order they appear, right after super(). |
| Constructor body | The remaining statements. If the constructor starts with this(...), the initializers run only in the constructor that calls super(...). |
Usage notes
- Forward references. A field initializer can not read a field declared later by simple name; the compiler rejects it. Using
this.xsidesteps that check and sees the default value, which is a common source of confusion. - Never call overridable methods from a constructor. In the second example,
describe()runs inBase's constructor beforeDerived's initializers:labelisnullandboxedisnull, while thefixedconstant already reads 42 because it is a compile-time constant. - Compile-time constants don't initialize the class. Reading a
static finalprimitive orStringconstant initialized with a constant expression is inlined by the compiler. - Exceptions in static initializers wrap into
ExceptionInInitializerError, and the class is unusable afterwards (NoClassDefFoundErroron later use). Keep static initialization simple. - Circular static dependencies between two classes can expose default values. Avoid cross-class static initialization chains.
- Singleton holder idiom. A private static nested class with a static field gives lazy, thread-safe initialization with no locking code, because class initialization is already synchronized.
- Enums and interfaces. Enum constants are created during the enum's class initialization. Interface fields are initialized only when accessed.
- Initialization of final instance fields must complete in every constructor; use one main constructor and
this(...)to keep it simple.