Java Initialization Block and the order of its execution.

In Java we have two different types of Initialization Block.

Types Of Block
We know instance block are used to intialize class fields but in this tutorial we will see the initialization block and constructor block and the order of its execution.

Order
Rule.
1. Static block will execute when the class is loaded into the JVM. Also it will execute only once.
2. If more than one static block appears, then it will execute in the order they appear in the class.
3. Instance Initialization block executes everytime when the instance is created.
4. If more than one Instance Initialization block appears, then it will execute in the order they appear in the class.
5. Constructor block will execute immediately after Instance Initialization block complete its execution.

Let’s see some of the examples and the Output.

Developer.java


package blocks;

public class Developer{
 
                          public Developer() {
                                 // TODO Auto-generated constructor stub
                                 System.out.println("NO ARG Developer Constructor");
                          }
                          public Developer(String a) {
                                 // TODO Auto-generated constructor stub
                                 System.out.println("String ARG Developer Constructor :" +a);
                          }
                          static{
                                 System.out.println("First STATIC Block in Developer");
                          }
                          static{
                                 System.out.println("Second STATIC Block in Developer");
                          }
                          {
                                 System.out.println("First INSTANCE Block in Developer");
                          }
                          {
                                 System.out.println("Second INSTANCE Block in Developer");
                          }
                          public static void main(String[] args) {
                                 new Developer();
                                 new Developer();
                                 new Developer("hi developer");
                          }
}


Output will be


First STATIC Block in Developer
Second STATIC Block in Developer
First INSTANCE Block in Developer
Second INSTANCE Block in Developer
NO ARG Developer Constructor
First INSTANCE Block in Developer
Second INSTANCE Block in Developer
NO ARG Developer Constructor
First INSTANCE Block in Developer
Second INSTANCE Block in Developer
String ARG Developer Constructor :hi developer


Explanation of the output:

  • If you notice the output that the static block content are printed first and it has printed only once, because static blocks are belongs to class not for instances. So once the class is loaded into the JVM, static blocks will execute. Also you might notice that if more than one static block is appearing then the order of the static block is from top to bottom.
  • Whenever we try to create the instance for Developer, instance blocks will be executed. In our code we created the instance for 3 times (i.e new Developer(),new Developer(),new Developer(“hi developer”)). So instance blocks will execute thrice. Also similar to static block, the order of the instance block is from top to bottom, if more than one instance block is appearing.
  • In our code we have 2 no- arg constructor (i.e new Developer()) and 1 String argument constructor (i.e new Developer(“hi developer”)).  So whenever we try to create the instance for Developer,Initially instance blocks will be executed, once instance block execution completes, then immediately the control will go to it’s corresponding constructor block and completes its execution.

We have seen the order of execution for a single class. But what will be the order of execution, if a class has extended some other class.

TeamLead.java


package blocks;

public class TeamLead {

                         TeamLead(){
                                                   System.out.println("NO ARG TeamLead Constructor");
                         }
                         static{
                                     System.out.println("First STATIC Block in TeamLead");
                         }
                         static{
                                     System.out.println("Second STATIC Block in TeamLead");
                         }
 
                         {
                                     System.out.println("First INSTANCE Block in TeamLead");
                         }
 
                         {
                                     System.out.println("Second INSTANCE Block in TeamLead");
                         }
}


Developer.java

</pre>
<pre>
package blocks;

public class Developer{
 
                          public Developer() {
                                 // TODO Auto-generated constructor stub
                                 System.out.println("NO ARG Developer Constructor");
                          }
                          public Developer(String a) {
                                 // TODO Auto-generated constructor stub
                                 System.out.println("String ARG Developer Constructor :" +a);
                          }
                          static{
                                 System.out.println("First STATIC Block in Developer");
                          }
                          static{
                                 System.out.println("Second STATIC Block in Developer");
                          }
                          {
                                 System.out.println("First INSTANCE Block in Developer");
                          }
                          {
                                 System.out.println("Second INSTANCE Block in Developer");
                          }
                          public static void main(String[] args) {
                                 new Developer();
                                 new Developer();
                                 new Developer("hi developer");
                          }
}</pre>
<pre>

Output will be


First STATIC Block in TeamLead
Second STATIC Block in TeamLead
First STATIC Block in Developer
Second STATIC Block in Developer
First INSTANCE Block in TeamLead
Second INSTANCE Block in TeamLead
NO ARG TeamLead Constructor
First INSTANCE Block in Developer
Second INSTANCE Block in Developer
NO ARG Developer Constructor
First INSTANCE Block in TeamLead
Second INSTANCE Block in TeamLead
NO ARG TeamLead Constructor
First INSTANCE Block in Developer
Second INSTANCE Block in Developer
NO ARG Developer Constructor
First INSTANCE Block in TeamLead
Second INSTANCE Block in TeamLead
NO ARG TeamLead Constructor
First INSTANCE Block in Developer
Second INSTANCE Block in Developer
String ARG Developer Constructor :hi developer<strong>
</strong>

Explanation of the Output:

  • We know that the static block will be executed when the class is loaded, in our case Developer class extended the TeamLead class so both the classes loaded into the JVM and the content will be printed first. Initially TeamLead class static block content will be printed followed by Developer class static block content will be printed.
  • We know when we try to create an instance of a class, constructor of a actual class and its superclass will be executed. [To Know More :15 things to know about Java Constructor]. So when we create an instance, initially super class instance block will be executed, once the super class instance block is completed then immediately the control will go to super class constructor and execute the block, followed by sub class instance block will be executed and finally sub class constructor will be executed.

Leave a comment

Your email address will not be published. Required fields are marked *