Initialization block in Java

Just as constructors and methods, initialization blocks also add up to places where an operation can be performed. So there are few rules to keep in mind when dealing with the initialization blocks. But before we do that, we must know that there are two types of initialization block in Java, namely –

  1. Static initialization block
  2. Instance initialization block

The wonderful thing about these initialization blocks is that they don’t have a name, they don’t even take any arguments and they also don’t return anything. 

Static initialization block starts with a ‘static’ keyword followed by opening and closing curly brace. The operation that needs to be performed goes inside this block. Below mentioned is a typical static initialization block.

Similar to the static initialization block is the instance initialization block, which DOESN’T has any ‘static’ keyword and is delimited with just an opening and a closing curly brace. Below mentioned is a typical instance initialization block.

Now let’s get to the point as to when do these blocks execute in a java program.

  • Static initialization block executes as soon as the class gets loaded.
  • Instance initialization block executes whenever the class is instantiated, right after the call to super() method of the constructor.

Let’s check out a simple java program that demonstrates the behavior of both the types of initialization block.

When we run the above program, the output that we get is –


Explanation

The first four lines of the output is from the static blocks in the same order in which they have been defined.

When the program is run, and class B is instantiated, the default constructor of B is invoked. The first line of the default constructor has an implicit call to default constructor of the super class A via super(). This calls the default constructor of class A i.e. A().

The first line of the default constructor A() also has an implicit call to default constructor of the super class A (if any) via super(). Right after the call to super(), the initialization blocks execute in the order of their definition in the code. Hence is the 5th line in the output. Once all the initialization blocks of class A execute, rest of the default constructor A() code executes, thus printing the 6th line in the output.

Right after the call to super() finishes, the control returns to the default constructor of class B i.e. B(). Now the initialization blocks of class B execute in the order of their definition in the code. Hence are the 7th and 8th lines in the output. Once the initilization block execution finishes, finally the rest of the default constructor B() code executes, thus printing the 9th line in the output.

I hope the flow is clear with the explanation. But before we wrap up, there are a couple of points note:

  • Static initialization blocks run only once, when the class is first loaded.
  • Instance initialization block run everytime the class is instantiated, right after the call to super() method of the constructor.

Receive our updates to your inbox

Get more stuff like this
in your inbox

Subscribe to our mailing list and get interesting stuff and updates to your email inbox.