Instance Initialization Block (IIB) in Java
Last Updated :
18 Oct, 2022
In a Java program, operations can be performed on methods, constructors, and initialization blocks. Instance Initialization Blocks or IIBs are used to initialize instance variables. So firstly, the constructor is invoked and the java compiler copies the instance initializer block in the constructor after the first statement super(). They run each time when the object of the class is created.
- Initialization blocks are executed whenever the class is initialized and before constructors are invoked.
- They are typically placed above the constructors within braces.
- It is not at all necessary to include them in your classes.
Java
// Java program to illustrate
// Instance Initialization Block
class GfG {
// Instance Initialization Block
{
System.out.println("IIB block");
}
// Constructor of GfG class
GfG() { System.out.println("Constructor Called"); }
public static void main(String[] args)
{
GfG a = new GfG();
}
}
OutputIIB block
Constructor Called
Multiple Instance Initialization Blocks in a Program
We can also have multiple IIBs in a single class. If the compiler finds multiple IIBs, then they all are executed from top to bottom i.e. the IIB which is written at the top will be executed first.
Java
// Java program to illustrate
// execution of multiple
// Instance Initialization Blocks
// in one program
class GfG {
// Instance Initialization Block - 1
{
System.out.println("IIB1 block");
}
// Instance Initialization Block - 2
{
System.out.println("IIB2 block");
}
// Constructor of class GfG
GfG() { System.out.println("Constructor Called"); }
// Instance Initialization Block - 3
{
System.out.println("IIB3 block");
}
// main function
public static void main(String[] args)
{
GfG a = new GfG();
}
}
OutputIIB1 block
IIB2 block
IIB3 block
Constructor Called
Instance Initialization Block with parent class
You can have IIBs in parent class also. Instance initialization block code runs immediately after the call to super() in a constructor. The compiler executes the parent's class's IIB before executing the current class's IIBs.
Have a look at the following example.
Java
// Java program to illustrate
// Instance Initialization Block
// with super()
// Parent Class
class B {
B() { System.out.println("B-Constructor Called"); }
{
System.out.println("B-IIB block");
}
}
// Child class
class A extends B {
A()
{
super();
System.out.println("A-Constructor Called");
}
{
System.out.println("A-IIB block");
}
// main function
public static void main(String[] args)
{
A a = new A();
}
}
OutputB-IIB block
B-Constructor Called
A-IIB block
A-Constructor Called
In the above example, the compiler tries to execute the class A constructor, when the object of class A is created. But it finds super() statement and goes to the parent class constructor first to be executed. The order of execution, in this case, will be as follows:
- Instance Initialization Block of the superclass.
- Constructors of the superclass.
- Instance Initialization Blocks of the subclass.
- Constructors of the subclass.
Important points:
- Instance Initialization Blocks run every time a new instance is created.
- Initialization Blocks run in the order they appear in the program
- The Instance Initialization Block is invoked after the parent class constructor is invoked (i.e. after super() constructor call)
Related Article :
The Initializer Block in Java
Similar Reads
Double Brace Initialization in Java The combination of two separate processes in Java is known as Double Brace Initialization in Java. As the name suggests, there are two braces {{ included in it. A single brace { is nothing new for programmers. The first brace in the double brace initialization is used to create an anonymous inner cl
4 min read
The Initializer Block in Java In order to perform any operations while assigning values to an instance data member, an initializer block is used. In simpler terms, the initializer block is used to declare/initialize the common part of various constructors of a class. It runs every time whenever the object is created. The initial
2 min read
How to Execute Instance Initialization Block (IIB) without Creating Object in Java? In a Java program, operations can be performed on methods, constructors, and initialization blocks. Instance Initialization Blocks or IIB are used to initialize instance variables. We know that the instance block is the name-less method in java inside which we can define logic and they possess certa
3 min read
Using Instance Blocks in Java The instance block can be defined as the name-less method in java inside which we can define logic and they possess certain characteristics as follows. They can be declared inside classes but not inside any method. Instance block logic is common for all the objects. Instance block will be executed o
3 min read
Initialization of local variable in a conditional block in Java Java comprises 5 conditional blocks namely - if, switch, while, for and try. In all these blocks, if the specified condition is true, the code inside the block is executed and vice-versa. Also, Java compiler doesn't let you leave a local variable uninitialized. While initializing local variable insi
3 min read
BlockingQueue Interface in Java The BlockingQueue Interface in Java is a part of the java.util.concurrent package and was introduced in Java 1.5. It is a specialized Queue implementation designed to handle thread-safe operations in concurrent environments. BlockingQueue is commonly used in Producer-Consumer patterns and other mult
11 min read