Instance Initializer Blocks and Static Blocks

A block statement is a sequence of zero or more statements enclosed in braces. A block statement is generally used to group together several statements, so they can be used in a situation that requires us to use a single statement.

We can write the blocks as other class members such as fields and methods of a class. All the variables declared in a block statement can only be used within that block. In other words, we can say that all variables declared in a block have local scope.

There are two types of blocks in Java— Instance initializer block and Static block.

1) Instance initializer block: It contains the code that is always executed whenever an instance of a class is created. It is mainly used to declare/initialize the instance data members of a class (though static members can also be accessed). The initializer block is executed before the constructor’s code executes.

2) Static block: It contains the code that is always executed whenever a class is loaded into the JVM. It is used to initialize the static data members of a class. As expected, the static block is executed before the constructor’s code executes.

 

Example 1

The following example demonstrates different types of Java blocks in the same program. At first, the static block gets executed, followed by the instance initializer block and finally the constructor.

BlockTest.java

class Abc {
int a; //instance field
static int b; //class field

//constructor
Abc() {
a = 10; b = 20;
System.out.println("constructor");
}

//instance initializer block
{
a = 30; b = 40;
System.out.println("instance initializer block");
}

//static block
static {
b = 30;
System.out.println("static block");
}
}

public class BlockTest {

public static void main(String[] args) {
Abc obj = new Abc();
System.out.println("a = " + obj.a);
System.out.println("b = " + Abc.b);
}
}

Output:

static block
instance initializer block
constructor
a = 10
b = 20

 

 

Example 2

In the example below we have two instance initializer blocks and two static blocks. These blocks come in the order in which they appear in the program. See the code below.

BlocksTest.java

class Block {

Block() {
System.out.println("constructor");
}

{
System.out.println("instance initializer block 1");
}

{
System.out.println("instance initializer block 2");
    }

static {
System.out.println("static block 1");
}

static {
System.out.println("static block 2");
}
}

public class BlocksTest {

public static void main(String[] args) {
new Block();
}
}

Output:

static block 1
static block 2
instance initializer block 1
instance initializer block 2
constructor

 

 

Example 3

The following program shows that during the subclass object creation its instance initializer block is invoked after the super class constructor being called implicitly. Another point is that the static block of superclass First is called before the static block of subclass Second whenever these classes are loaded into the JVM following the superclass-subclass order.

BlocksInheritTest.java

class First { //superclass

First() {
System.out.println("parent constructor");
}

{
System.out.println("parent instance initializer block");
}

static {
System.out.println("parent static block");
}
}

class Second extends First { //subclass

Second() {
System.out.println("child constructor");
}

{
System.out.println("child instance initializer block");
}

static {
System.out.println("child static block");
}
}

public class BlocksInheritTest {

public static void main(String[] args) {

new Second(); //subclass object

}
}

Output:

parent static block
child static block
parent instance initializer block
parent constructor
child instance initializer block
child constructor