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

5.1K views 3 minutes read
A+A-
Reset

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.

[clickToTweet tweet=”What is Static Block, Non-static Block and Constructors in class in Java & their execution order.” quote=”Any code written inside class with static keyword is called static block. @PRsBlog #Java #Article #Blocks” theme=”style4″]

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

Related Posts

Leave a Reply

1 comment

priya May 9, 2018 - 11:27 AM

Thank you for the valuable post about static method as i was in confusion but now its clear , It is going to help me a lot. Hope you will keep on posting to update us

Reply

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More

Adblock Detected

Please support us by disabling your AdBlocker extension from your browsers for our website.