Java Top

Get started with Spring 5 and Spring Boot 2, through the Learn Spring course:

> CHECK OUT THE COURSE

1. Overview

In this tutorial, we'll learn the concept of static block and instance initializer block. We'll also check the differences and the execution order of the class constructors and initializer blocks.

2. Static Block

In Java, a static block executes code before the object initialization. A static block is a block of code with a static keyword:

static {
    // definition of the static block
}

Static initializer block or static initialization block, or static clause are some other names for the static block. Static block code executes only once during the class loading. The static blocks always execute first before the main() method in Java because the compiler stores them in memory at the time of class loading and before the object creation.

A class can have multiple static blocks, and they will execute in the same order as they appear in the class:

public class StaticBlockExample {

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

    public static void main(String[] args) {
        System.out.println("Main Method");
    }
}

The output for the above code snippet is:

static block 1
static block 2
Main Method

Here, the compiler executes all the static blocks first, and after finishing the static block execution, it invokes the main() method. The Java compiler makes sure that the execution of static initialization blocks will be in the same sequence as they appear in the source code.

Static blocks of parent class execute first because the compiler loads parent class before child class.

As a curiosity, before Java 1.7, the main() method wasn't mandatory in every Java application, so all the code could be written within static blocks. However, from Java 1.7 onwards, the main() method is mandatory.

3. Instance Initializer Block

As the name suggests, the purpose of the instance initializer block is to initialize the instance data members.

The instance initializer block looks just like the static initializer block, but without the static keyword:

{
     // definition of the Instance initialization block
}

Static initializer blocks always execute before the instance initialization blocks because static blocks run at the time of class loading. However, the instance block runs at the time of instance creation. The Java compiler copies initializer blocks into every constructor. Therefore, multiple constructors can use this approach to share a block of code:

public class InstanceBlockExample {

    {
        System.out.println("Instance initializer block 1");
    }
    
    {
        System.out.println("Instance initializer block 2");
    }
    
    public InstanceBlockExample() {
        System.out.println("Class constructor");
    }

    public static void main(String[] args) {
        InstanceBlockExample iib = new InstanceBlockExample();
        System.out.println("Main Method");
    }
}

So, in this case, the output for the above code would be:

Instance initializer block 1
Instance initializer block 2
Class constructor
Main Method

The instance initializer blocks execute during every constructor invocation since the compiler copies the initializer block in the constructor itself.

The compiler executes the parent class's instance block before executing the current class's instance block. The compiler invokes the parent class constructor by super(), and instance blocks execute at the time of constructor invocation.

4. Differences Between Static and Instance Initializer Block

Static Block Instance Initializer Block
It executes during class loading It executes during class instantiation
It can only use static variables It can use static or non-static (instance variables).
It can not use this It can use this
It executes only once during the entire execution of the program when the class loads into the memory It can run many times whenever there is a call to the constructor

5. Conclusion

In this tutorial, we have learned that the compiler executes static blocks during class loading. Static blocks can be used to initialize static variables or to call a static method. However, an instance block is executed every time an instance of the class is created, and it can be used to initialize the instance data members.

In addition, the complete code samples for this article can be found over on GitHub.

Java bottom

Get started with Spring 5 and Spring Boot 2, through the Learn Spring course:

>> CHECK OUT THE COURSE
Generic footer banner
guest
0 Comments
Inline Feedbacks
View all comments