instance initializer vs static initializer block in java

In this post, we will learn the difference between "Instance Initializer" and "Static Initializer" blocks
in Java. This is a frequently asked question in Java interviews for beginners. Let's dive into it.

Difference between "Instance Initializer" and "Static Initializer" blocks in Java

Feature Instance Initializer Block Static Initializer Block
Placement Defined within a class but outside any method, constructor, or static block. Defined within a class but outside any method, constructor, or other static block.
Execution Executed every time an object of the class is created. Executed only once when the class is loaded into memory.
Use Case Used to initialize instance variables of the class. Used to initialize static variables of the class.
Access to Variables Can access both instance variables and static variables of the class. Can access only static variables of the class.
Access to Methods Can access both instance methods and static methods of the class. Can access only static methods of the class.
Usage Used for per-object initialization tasks. Used for one-time initialization tasks for the entire class.
Inheritance Each subclass can have its own instance initializer block. Only the superclass static initializer block is inherited by the subclasses.
Order of Execution Instance initializer blocks are executed in the order they appear in the code during object creation. Static initializer blocks are executed in the order they appear in the code during class loading.
Multiple Initialization Multiple instance initializer blocks are executed in sequence before the constructor. Multiple static initializer blocks are executed in sequence.
Exception Handling Can throw checked or unchecked exceptions. Can throw only unchecked exceptions.

Example

Let's create an example to demonstrate the difference between instance initializer and static initializer block in Java. 

Instance Initializer: 

An instance initializer is a block of code enclosed in curly braces and is defined within a class. It is executed when an instance of the class is created, right before the constructor is called. Instance initializer blocks are used to initialize instance variables of the class.
public class InstanceInitializerExample {
    private int instanceVar;

    // Instance Initializer Block
    {
        System.out.println("Instance initializer block is executed.");
        instanceVar = 10;
    }

    public InstanceInitializerExample() {
        System.out.println("Constructor is called.");
    }

    public static void main(String[] args) {
        InstanceInitializerExample obj = new InstanceInitializerExample();
        System.out.println("Instance variable value: " + obj.instanceVar);
    }
}

Output:

Instance initializer block is executed.
Constructor is called.
Instance variable value: 10

Static Initializer: 

A static initializer is a block of code enclosed in curly braces and is defined within a class with the static keyword. It is executed only once when the class is loaded into the memory before any static methods or variables are accessed. Static initializer blocks are used to initialize static variables of the class.

public class StaticInitializerExample {
    private static int staticVar;

    // Static Initializer Block
    static {
        System.out.println("Static initializer block is executed.");
        staticVar = 20;
    }

    public static void main(String[] args) {
        System.out.println("Static variable value: " + StaticInitializerExample.staticVar);
    }
}

Output:

Static initializer block is executed.
Static variable value: 20

Comments