Java initialization order is one of those topics that looks simple until a class contains static variables, static blocks, instance variables, instance initialization blocks, constructors, and inheritance. Then the order becomes critical.
Understanding this order helps you predict exactly which value a variable has at each stage and explains many common interview questions about object creation.
Why Does Initialization Order Matter?
Consider a class where several pieces of code run automatically during class loading and object creation. Java cannot execute all of them at once. It follows a defined sequence.
A useful mental model is:
Class Initialization
↓
Static Variables
↓
Static Blocks
↓
Object Initialization
↓
Instance Variables
↓
Instance Initialization Blocks
↓
Constructor
This simplified flow is especially useful for understanding a standalone class. In inheritance, the parent class participates in initialization before the child class.
Initialization Order in a Simple Class
Start with a class containing an instance variable, an instance initialization block, and a constructor.
class Student {
int age = 20;
{
System.out.println("Instance Block");
}
Student() {
System.out.println("Constructor");
}
}
public class Main {
public static void main(String[] args) {
Student student = new Student();
}
}
Instance Block Constructor
The assignment age = 20 happens before the instance initialization block. The block then executes, followed by the constructor body.
Static Initialization Order
Static initialization belongs to the class rather than to individual objects. Static field initializers and static blocks execute during class initialization in their source-code order.
class Demo {
static int first = initializeFirst();
static {
System.out.println("Static Block");
}
static int second = initializeSecond();
static int initializeFirst() {
System.out.println("First Static Variable");
return 10;
}
static int initializeSecond() {
System.out.println("Second Static Variable");
return 20;
}
}
public class Main {
public static void main(String[] args) {
Demo demo = new Demo();
}
}
First Static Variable Static Block Second Static Variable
The important lesson is that Java does not execute all static fields first and all static blocks afterward. Instead, their initialization follows the order in which they appear in the class.
Static Members Follow Source Order
Consider this example:
class Configuration {
static int value = 10;
static {
value = 20;
}
static int result = value * 2;
}
The sequence is:
value = 10
↓
value = 20
↓
result = value * 2
↓
result = 40
The final value of result is therefore 40, not 20.
Instance Initialization Order
When an object is created, Java initializes the instance state before executing the constructor body.
class Product {
int price = 100;
{
price = 200;
System.out.println("Instance Block: " + price);
}
Product() {
System.out.println("Constructor: " + price);
}
}
Instance Block: 200 Constructor: 200
The field initializer first assigns 100. The instance block changes it to 200. The constructor then sees the updated value.
Instance Members Follow Source Order Too
Instance field initializers and instance initialization blocks execute in their textual order.
class Demo {
int number = 10;
{
number = 20;
}
int result = number + 5;
Demo() {
System.out.println(result);
}
}
25
The execution is:
number = 10
↓
number = 20
↓
result = 20 + 5
↓
result = 25
↓
Constructor
This is why source-code order matters when initialization expressions depend on previously initialized fields.
Complete Order in a Single Class
Now combine static and instance initialization into one example.
class Demo {
static int staticValue = initializeStatic();
static {
System.out.println("Static Block");
}
int instanceValue = initializeInstance();
{
System.out.println("Instance Block");
}
Demo() {
System.out.println("Constructor");
}
static int initializeStatic() {
System.out.println("Static Variable");
return 10;
}
int initializeInstance() {
System.out.println("Instance Variable");
return 20;
}
}
public class Main {
public static void main(String[] args) {
Demo demo = new Demo();
}
}
Static Variable Static Block Instance Variable Instance Block Constructor
This example gives us a strong foundation: class-level initialization happens first, followed by object-level initialization.
What Happens When Multiple Objects Are Created?
The difference between static and instance initialization becomes obvious when multiple objects are created.
class Counter {
static {
System.out.println("Static Block");
}
{
System.out.println("Instance Block");
}
Counter() {
System.out.println("Constructor");
}
}
public class Main {
public static void main(String[] args) {
Counter c1 = new Counter();
Counter c2 = new Counter();
}
}
Static Block Instance Block Constructor Instance Block Constructor
The static block runs once during class initialization. The instance block and constructor run for every object.
| Component | Execution Frequency |
|---|---|
| Static field initializer | During class initialization |
| Static block | Normally once per class initialization |
| Instance field initializer | Once per object |
| Instance initialization block | Once per object |
| Constructor body | Once per object |
Initialization Order with Constructor Chaining
Constructor chaining does not change the fact that instance initialization happens before the constructor body of the constructor that begins the chain.
class Student {
int age = 18;
{
System.out.println("Instance Block");
}
Student() {
this("Unknown");
System.out.println("No-Argument Constructor");
}
Student(String name) {
System.out.println("Parameterized Constructor");
}
}
public class Main {
public static void main(String[] args) {
Student student = new Student();
}
}
Instance Block Parameterized Constructor No-Argument Constructor
The instance initialization occurs before the constructor chain's constructor bodies execute. The this() call then delegates to the parameterized constructor, after which control returns to the original constructor.
Initialization Order with Inheritance
Inheritance adds another important rule: when an object of a child class is created, the parent portion must be initialized before the child portion.
class Parent {
static {
System.out.println("Parent Static Block");
}
{
System.out.println("Parent Instance Block");
}
Parent() {
System.out.println("Parent Constructor");
}
}
class Child extends Parent {
static {
System.out.println("Child Static Block");
}
{
System.out.println("Child Instance Block");
}
Child() {
System.out.println("Child Constructor");
}
}
public class Main {
public static void main(String[] args) {
Child child = new Child();
}
}
Parent Static Block Child Static Block Parent Instance Block Parent Constructor Child Instance Block Child Constructor
This is one of the most important initialization sequences to remember. Before Java can initialize the child portion of the object, the parent class must first be initialized and the parent part of the object must be constructed.
Why Does the Parent Initialize First?
A child object contains the inherited state of its parent as well as its own state. Java therefore establishes the parent portion first and then continues with the child portion.
Child Object
|
+-- Parent State
|
+-- Child State
The parent constructor is therefore completed before the child constructor body begins.
Static Initialization in an Inheritance Hierarchy
When a child class is actively initialized, its relevant superclass must be initialized first.
class A {
static {
System.out.println("A Static");
}
}
class B extends A {
static {
System.out.println("B Static");
}
}
class C extends B {
static {
System.out.println("C Static");
}
}
public class Main {
public static void main(String[] args) {
C object = new C();
}
}
A Static B Static C Static
The initialization proceeds from the top of the inheritance hierarchy toward the child class.
Complete Inheritance Initialization Flow
For a child object in a simple inheritance hierarchy, a useful high-level sequence is:
Parent Class Initialization
↓
Child Class Initialization
↓
Parent Instance Initialization
↓
Parent Constructor
↓
Child Instance Initialization
↓
Child Constructor
This sequence is an excellent interview memory aid, but remember that field initializers and initialization blocks within each class follow their own source-code order.
A Complete Example
class Parent {
static int a = initializeA();
static {
System.out.println("Parent Static Block");
}
int x = initializeX();
{
System.out.println("Parent Instance Block");
}
Parent() {
System.out.println("Parent Constructor");
}
static int initializeA() {
System.out.println("Parent Static Variable");
return 10;
}
int initializeX() {
System.out.println("Parent Instance Variable");
return 20;
}
}
class Child extends Parent {
static int b = initializeB();
static {
System.out.println("Child Static Block");
}
int y = initializeY();
{
System.out.println("Child Instance Block");
}
Child() {
System.out.println("Child Constructor");
}
static int initializeB() {
System.out.println("Child Static Variable");
return 30;
}
int initializeY() {
System.out.println("Child Instance Variable");
return 40;
}
}
public class Main {
public static void main(String[] args) {
Child child = new Child();
}
}
Parent Static Variable Parent Static Block Child Static Variable Child Static Block Parent Instance Variable Parent Instance Block Parent Constructor Child Instance Variable Child Instance Block Child Constructor
This example brings the entire topic together. First, the parent class is initialized. Then the child class is initialized. Once the object begins instance initialization, the parent instance state is prepared first, followed by the parent constructor. Finally, the child instance state and child constructor complete object initialization.
Common Beginner Mistakes
Thinking Static Blocks Run for Every Object
They do not. Static initialization belongs to the class. Instance initialization belongs to individual objects.
Thinking the Constructor Runs First
The constructor body is not the first part of object initialization. Instance field initialization and instance initialization blocks occur before the constructor body.
Ignoring Source-Code Order
Static and instance initialization components generally execute according to their position within the class. Moving a field initializer or initialization block can change the resulting value.
Assuming Child Initialization Happens Before Parent Initialization
For a child object, the parent class is initialized first, and the parent instance portion is initialized before the child portion.
Confusing this() with Initialization Order
A this() call delegates to another constructor in the same class, but it does not make the instance initialization process start over. The object is initialized through one constructor chain.
Best Practices
- Keep initialization logic simple enough that its execution order is obvious.
- Prefer straightforward field initialization for simple values.
- Use constructors for initialization that depends on supplied arguments.
- Avoid complex side effects in initialization blocks.
- Be especially careful when static and instance members depend on values declared earlier in the class.
- When debugging initialization problems, trace class initialization first and object initialization second.
Remember: Static initialization prepares the class. Instance initialization prepares the object. The constructor body is the final part of that object's normal initialization sequence.
Interview Insight
A classic interview question asks: What is the order when a child object is created? The high-level answer is parent static initialization, child static initialization, parent instance initialization, parent constructor, child instance initialization, and child constructor.
Another common question asks why the parent constructor runs before the child constructor. The answer is that the inherited portion of the object must be initialized before the child portion can safely complete its initialization.
Quick Revision
| Stage | What Happens |
|---|---|
| Class Initialization | Static fields and static blocks execute in source order |
| Parent Class | Initialized before a child class |
| Instance Fields | Initialized during object initialization |
| Instance Blocks | Execute in source order before the constructor body |
| Parent Constructor | Initializes the parent portion of the object |
| Child Constructor | Initializes the child portion of the object |
| Multiple Objects | Static initialization does not repeat for each object; instance initialization does |
Final Thoughts
Java initialization order becomes much easier once you separate class initialization from object initialization. Static fields and static blocks prepare the class, while instance fields, initialization blocks, and constructors prepare each object. In inheritance, the parent always establishes its state before the child completes its own initialization. Mastering this sequence will make constructor chaining, static initialization, inheritance, and many tricky Java interview questions far easier to understand.
