Unlock Your Python Backend Career: Build 30 Projects in 30 Days. Join now for just $54

Object-Oriented Programming

Understanding Initialization Block in Java

Initialization Block

There are two types of instance blocks:

  • Instance Initialization Block (IIB)

  • Static Initialization Block (SIB): You can read more about static initialization block (SIB) after exploring the concept of inheritance in Java.

Instance Initialization Block (IIB)

Instance Initialization block is the block that runs whenever the object is created. Useful when a class has multiple constructors and some common tasks need to be executed for each constructor. It is executed after the default constructor and before any other constructor.

Syntax:

class class_name{
	{
		// write somthing
	}
}

Example:

class Person {
    private String name;
    private int age;

    {
        System.out.println("Instance Initialization Block Executed");
        this.name = "Default Name";
        this.age = 18;
    }

    public Person(String name, int age) {
        System.out.println("Parameterized Constructor Executed");
        this.name = name;
        this.age = age;
    }

    public Person() {
        System.out.println("Default Constructor Executed");
    }

    public void displayDetails() {
        System.out.println("Name: " + name + ", Age: " + age);
    }
}

public class Main {
    public static void main(String[] args) {
        System.out.println("Creating First Object...");
        Person person1 = new Person("Ayush", 25);
        person1.displayDetails();

        System.out.println("\nCreating Second Object...");
        Person person2 = new Person();
        person2.displayDetails();
    }
}

output:

Creating First Object...
Instance Initialization Block Executed
Parameterized Constructor Executed
Name: Ayush, Age: 25

Creating Second Object...
Instance Initialization Block Executed
Default Constructor Executed
Name: Default Name, Age: 18

Instance Initialization Blocks (IIB) execute before any constructor, making them useful for initializing common logic. Combining constructors this() and IIB ensures clean, efficient, and maintainable Java code.

Whenever you're ready

There are 4 ways we can help you become a great backend engineer:

The MB Platform

Join 1000+ backend engineers learning backend engineering. Build real-world backend projects, learn from expert-vetted courses and roadmaps, track your learnings and set schedules, and solve backend engineering tasks, exercises, and challenges.

The MB Academy

The “MB Academy” is a 6-month intensive Advanced Backend Engineering BootCamp to produce great backend engineers.

Join Backend Weekly

If you like post like this, you will absolutely enjoy our exclusive weekly newsletter, Sharing exclusive backend engineering resources to help you become a great Backend Engineer.

Get Backend Jobs

Find over 2,000+ Tailored International Remote Backend Jobs or Reach 50,000+ backend engineers on the #1 Backend Engineering Job Board