What is the order of execution of non-static blocks with respect to a constructor in Java?



In Java, non-static blocks run before the constructor whenever an object is created. If multiple non-static blocks exist, they execute in the order they appear in the class, followed by the constructor. In this article, we will understand this order of execution with the help of examples.

What Are Non-Static Blocks in Java?

The Non-static blocks are class-level blocks which don't contain a prototype. A non-static block must perform any logic whenever an object is instantiated, regardless of the constructor.

The Non-static blocks are automatically called by the JVM for every object creation in the Java stack area. We can create any number of Non-static blocks in Java.

Java Constructors

Java constructors are a special kind of method that is used to initialize an object when it is instantiated. It shares the same name as its class and is syntactically identical to a method. Constructors, however, do not have an explicit return type.

In this article, we'll understand the execution order of non-static blocks regarding a constructor of Java. A non-static block will always be executed before the execution of a constructor when an object is created.

Order of Execution of Non-Static Blocks and Constructors

The execution order is as below in Java when an object of a class is created:

  • Non-Static Blocks Execution: The execution order of non-static blocks is the order of their definition. If there are more non-static blocks, then these are called one after another before calling any constructor.
  • Constructor Execution: Once all non-static blocks have been run, the constructor is executed.
  • Static Blocks Execution: Static blocks are executed just once, as soon as the JVM loads the class first instead of during object creation.

Example of Order of Execution of Non-Static Blocks and Constructors

In the following example, we will see how non-static blocks are executed before the constructor:

public class NonStaticBlockTest {
   {
      System.out.println("First Non-Static Block"); // first non-static block
   }
   {
      System.out.println("Second Non-Static Block"); // second non-static block
   }
   {
      System.out.println("Third Non-Static Block"); // third non-static block
   }
   NonStaticBlockTest() {
      System.out.println("Execution of a Constructor"); // Constructor
   }
   public static void main(String args[]) {
      NonStaticBlockTest nsbt1 = new NonStaticBlockTest();
      NonStaticBlockTest nsbt2 = new NonStaticBlockTest();
   }
}

Following is the output of the above program:

First Non-Static Block
Second Non-Static Block
Third Non-Static Block
Execution of a Constructor
First Non-Static Block
Second Non-Static Block
Third Non-Static Block
Execution of a Constructor

Non-static Block With Multiple Constructors

Let's see how non-static blocks are executed when there are multiple constructors in a class:

Example

In the example below, we will create a class with two constructors and a non-static block. The non-static block will be executed before each constructor:

public class NonStaticBlockWithMultipleConstructors {
   {
      System.out.println("Non-Static Block");
   }
   NonStaticBlockWithMultipleConstructors() {
      System.out.println("Constructor 1");
   }
   NonStaticBlockWithMultipleConstructors(int x) {
      System.out.println("Constructor 2");
   }
   public static void main(String args[]) {
      NonStaticBlockWithMultipleConstructors obj1 = new NonStaticBlockWithMultipleConstructors();
      NonStaticBlockWithMultipleConstructors obj2 = new NonStaticBlockWithMultipleConstructors(5);
   }
}

Following is the output of the above program:

Non-Static Block
Constructor 1
Non-Static Block
Constructor 2
Alshifa Hasnain
Alshifa Hasnain

Converting Code to Clarity

Updated on: 2025-08-26T16:34:46+05:30

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements