Understanding the Execution Order of Static Blocks, Instance Blocks, Constructors, and Regular Code Blocks in Java
This article explains the definition, execution timing, and practical uses of Java static code blocks, instance (constructor) code blocks, constructors, and regular code blocks, and demonstrates their exact execution order—including parent‑child class initialization—through clear code examples and output analysis.
In Java, the execution order of static code blocks, instance (constructor) code blocks, constructors, and regular code blocks is a frequent interview topic; this article clarifies each component and their sequence.
1. Static Code Block
A static block is declared with the static keyword inside a class (not inside a method). It runs once when the class is loaded, before any other blocks or constructors.
public class CodeBlock {
static {
System.out.println("Static code block");
}
}Static blocks are useful for one‑time initialization such as loading configuration files.
2. Instance (Constructor) Code Block
An instance block is declared with plain braces { } inside a class. It executes every time an object is created, before the constructor runs.
public class CodeBlock {
static {
System.out.println("Static code block");
}
{
System.out.println("Instance code block");
}
}If multiple instance blocks exist, they run in the order they appear in the source file.
3. Constructors
A constructor must have the same name as its class, cannot declare a return type, and is invoked automatically when an object is created via new . It can be overloaded; each overload runs according to the arguments supplied.
4. Regular Code Block
A regular code block appears inside a method body and follows the same execution order as written in the method.
public void sayHello() {
{
System.out.println("Regular code block");
}
}5. Overall Execution Order
The combined order is: static block → instance block → constructor → regular block.
public class CodeBlock {
static {
System.out.println("Static code block");
}
{
System.out.println("Instance code block");
}
public CodeBlock() {
System.out.println("No‑arg constructor");
}
public void sayHello() {
{
System.out.println("Regular code block");
}
}
public static void main(String[] args) {
System.out.println("Running main");
new CodeBlock().sayHello();
System.out.println("---------------");
new CodeBlock().sayHello();
}
}Running the program shows the static block executes only once, while the instance block, constructor, and regular block execute on each object creation.
6. Parent‑Child Class Execution Order
When a subclass is instantiated, the JVM first runs the parent class’s static block, then the child’s static block, followed by the parent’s instance block and constructor, and finally the child’s instance block and constructor.
package com.ys.extend;
public class SuperClass {
static { System.out.println("Parent static block"); }
{ System.out.println("Parent instance block"); }
public SuperClass() { System.out.println("Parent constructor"); }
} package com.ys.extend;
public class SubClass extends SuperClass {
static { System.out.println("Child static block"); }
{ System.out.println("Child instance block"); }
public SubClass() { System.out.println("Child constructor"); }
} public static void main(String[] args) {
SubClass sb = new SubClass();
System.out.println("------------");
SubClass sb1 = new SubClass();
}The output confirms the described order, with static blocks executing only once and instance blocks/constructors executing for each object.
IT Services Circle
Delivering cutting-edge internet insights and practical learning resources. We're a passionate and principled IT media platform.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.