Static Blocks, Non-Static Blocks and Constructor Execution Order in Java


Hello guys. Here we’ll try to illustrate in which order do static blocks, non static blocks and constructor executes in java.

Any code written inside class with static keyword is called static block. This static block is class level code. So whenever Class Loader loads the class in jvm the static code in that class is executed. So we should be clear that the static block will be executed only once at time of loading of class. It will be executed before any code in the class. If you have any super class which contains static block too, then the static blocks of the super class will be executed first. We know that higher level classes are loaded first.

Any code written inside class with static keyword is called static block. @PRsBlog #Java #Article #BlocksClick To Tweet

The code in class with out any keyword are non static block. Non static blocks are also called initializer blocks because non static blocks are executed when object is initialized i.e. when constructor body is executed. But if we have super class, then constructor of super class is executed. Execution of super class constructor is part of object creation  and execution of sub class constructor is part of object initialization. So as the name implies, initializer block execute after super class constructor but before sub class constructor.

Now we can conclude that->

  • First Static block is executed (class loading )
  • Then super class constructor is executed(object creation).
  • Next initializer block will be executed(before object initialization)
  • Then constructor will be executed(object initialization )

Lets have a look at following code Download code here.

package blocksOrder;

public class Test extends Supper {

	Test()// constructor 
	{
		System.out.println("constructor");
	}
	static // Static block
	{
		System.out.println("static");
	}
         // Non static block
	{
		System.out.println("nonstatic");
	}
 
    
public static void main(String[] args)
{
	Test t = new Test();
	System.out.println("Main method");
	Test t1=new Test();

}

}

Also a super  class.

package blocksOrder;

public class Supper {
        {
               System.out.println("non static supper");
          }
	static{ //static block
		System.out.println("Static of supper");
	}
	Supper(){
		System.out.println("Supper class constructor");
	}

}

Here output will be:

Static of supper
static
non static supper
Supper class constructor
nonstatic
constructor
Main method
non static supper
Supper class constructor
nonstatic
constructor

Static and not static blocks output

Lets summarize Static blocks, Non static blocks and Constructor order of execution again.

  1. Static of super class
  2. Static of class
  3. While creating object each time
    • Super class non static block
    • Super class constructor
    • class non static block
    • class constructor

Have any Question or Comment?

Leave a Reply

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