Order of Execution of Initialization Blocks and Constructors in Java
Last Updated :
12 May, 2025
In Java, there are various techniques, which we can use to initialize and perform operations on objects such as methods, constructors, and initialization blocks. These tools are used to ensure that the program works as expected.
- Instance Initialization Blocks (IIB) are used to initialize instance variables. IIBs are executed before constructors. They run each time when object of the class is created.
- Initializer block contains the code that is always executed whenever an instance is created. It is used to declare/initialize the common part of various constructors of a class.
- Constructors are used to initialize the object’s state. Like methods, a constructor also contains a collection of statements(i.e., instructions) that are executed at the time of Object creation.
Prerequisite: Static blocks, Initializer block, Constructor
Execution Order of Initialization Blocks and Constructors in Java
- Static initialization blocks will run whenever the class is loaded first time in JVM.
- Initialization blocks run in the same order in which they appear in the program.
- Instance Initialization blocks are executed whenever the class is initialized and before constructors are invoked. They are typically placed above the constructors within braces.
- Constructors are called after all the instance initialization blocks
Note: If there are multiple static or Instance Initialization blocks then the execution will start in the same order in which they appear in the source code.
Example 1: In this example, we are demonstrating the execution sequence of static blocks, instance initialization blocks, and constructors when objects are created.
Java
// Java program to demonstrate the order of
// execution of constructors, static blocks,
// and instance initialization blocks
class Geeks {
Geeks(int x) {
System.out.println("ONE argument constructor");
}
Geeks() {
System.out.println("No argument constructor");
}
static {
System.out.println("1st static init");
}
{
System.out.println("1st instance init");
}
{
System.out.println("2nd instance init");
}
static {
System.out.println("2nd static init");
}
public static void main(String[] args) {
new Geeks();
new Geeks(8);
}
}
Output1st static init
2nd static init
1st instance init
2nd instance init
No argument constructor
1st instance init
2nd instance init
ONE argument constructor
Explanation: Here, when the class is loaded, the static block will execute first. After that the instance initialization blocks are executed before the constructor everytime an object is created and then the constructor is called.
If there are two or more static/initializer blocks then they are executed in the order in which they appear in the source code. Now, predict the output of the following program.
Example 2: In this example, we are showing how the order of static initializations and field assignments can affect the program output in a non-obvious way.
Java
// Java program to demonstrate tricky behavior
// of static initialization blocks and their
// order of execution
class MyTest {
static {
initialize();
}
private static int sum;
public static int getSum() {
initialize();
return sum;
}
private static boolean initialized = false;
private static void initialize() {
if (!initialized) {
for (int i = 0; i < 100; i++) {
sum += i;
}
initialized = true;
}
}
}
public class Geeks {
public static void main(String[] args) {
System.out.println(MyTest.getSum());
}
}
Explanation:
- Loop in initialize function goes from 0 to 99. With that in mind, you might think that the program prints the sum of the numbers from 0 to 99. Thus sum is 99 × 100 / 2, or 4950. The program, however, thinks otherwise. It prints 9900, fully twice this value.
- To understand its behavior, let’s trace its execution.The GFG.main method invokes MyTest.getSum. Before the getSum method can be executed, the VM must initialize the class MyTest. Class initialization executes static initializers in the order they appear in the source.
- The MyTest class has two static initializers: the static block at the top of the class and the initialization of the static field initialized. The block appears first. It invokes the method initialize, which tests the field initialized. Because no value has been assigned to this field, it has the default boolean value of false.
- Similarly, sum has the default int value of 0. Therefore, the initialize method does what you’d expect, adding 4, 950 to sum and setting initialized to true. After the static block executes, the static initializer for the initialized field sets it back to false, completing the class initialization of MyTest. Unfortunately, sum now contains the 4950, but initialized contains false.
- The main method in the GFG class then invokes MyTest.getSum, which in turn invokes initialize method. Because the initialized flag is false, the initializeIf method enters its loop, which adds another 4, 950 to the value of sum, increasing its value to 9, 900. The getSum method returns this value, and the program prints it.
Similar Reads
Java Constructors In Java, constructors play an important role in object creation. A constructor is a special block of code that is called when an object is created. Its main job is to initialize the object, to set up its internal state, or to assign default values to its attributes. This process happens automaticall
10 min read
Default constructor in Java Like C++, Java automatically creates default constructor if there is no default or parameterized constructor written by user, and (like C++) the default constructor automatically calls parent default constructor. But unlike C++, default constructor in Java initializes member data variable to default
1 min read
Private Constructors and Singleton Classes in Java In Java, the private constructor is a special type of constructor that cannot be accessed from outside the class. This is used in conjunction with the singleton design pattern to control the instantiation. Private ConstructorThe private constructor is used to resist other classes to create new insta
2 min read
Copy Constructor in Java Like C++, Java also supports a copy constructor. But, unlike C++, Java doesn't create a default copy constructor if you don't write your own. A prerequisite prior to learning copy constructors is to learn about constructors in java to deeper roots. Below is an example Java program that shows a simpl
4 min read
Constructor Chaining In Java with Examples Constructor chaining is the process of calling one constructor from another constructor with respect to current object. One of the main use of constructor chaining is to avoid duplicate codes while having multiple constructor (by means of constructor overloading) and make code more readable. Prerequ
5 min read
Why Constructors are not inherited in Java? Constructor is a block of code that allows you to create an object of class and has same name as class with no explicit return type. Whenever a class (child class) extends another class (parent class), the sub class inherits state and behavior in the form of variables and methods from its super clas
2 min read
Constructor Overloading in Java Java supports Constructor Overloading in addition to overloading methods. In Java, overloaded constructor is called based on the parameters specified when a new is executed. When do we need Constructor Overloading? Sometimes there is a need of initializing an object in different ways. This can be do
5 min read
Static Blocks in Java In simpler language whenever we use a static keyword and associate it to a block then that block is referred to as a static block. Unlike C++, Java supports a special block, called a static block (also called static clause) that can be used for static initialization of a class. This code inside the
4 min read
The Initializer Block in Java In order to perform any operations while assigning values to an instance data member, an initializer block is used. In simpler terms, the initializer block is used to declare/initialize the common part of various constructors of a class. It runs every time whenever the object is created. The initial
2 min read
Order of Execution of Initialization Blocks and Constructors in Java In Java, there are various techniques, which we can use to initialize and perform operations on objects such as methods, constructors, and initialization blocks. These tools are used to ensure that the program works as expected. Instance Initialization Blocks (IIB) are used to initialize instance va
4 min read