Instance Initialization Block (IIB) in Java

Instance Initialization Blocks or IIB are used to initialize instance variables. IIBs are executed before constructors. They run each time when 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 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(); 
	} 
} 

IIB block
Constructor Called
Multiple Instance Initialization Blocks in a Program

We can also have multiple IIBs in a single class. If compiler finds multiple IIBs, then they all are executed from top to bottom i.e. the IIB which is written at top will be executed first.

// 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(); 
	} 
} 

Output :

IIB1 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 parents class’s IIB before executing current class’s IIBs. Have a look on following example.

// 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(); 
	} 
} 

Output :

B-IIB block
B-Constructor Called
A-IIB block
A-Constructor Called

In above example, compiler tries to execute constructor of class A, when object of class A is created. But it finds super() statement and goes to the parent class constructor first to be executed. Order of execution in this case will be as follows:
I. Instance Initialization Block of super class
II. Constructors of super class
III. Instance Initialization Blocks of the class
IV. Constructors of the class

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)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值