What is IIB and SIB in Java?
IIB is Instance Initialization Block. SIB is Static Initialization Block.
IIB: Purpose of IIB is to initialize all non-static members in one place. It improves the readibility of program. When object of class is created it is called.By using this our code looks clean. We use it in program By using curly brackets. When we write this in program where constructor already exists, IIB have first priorty.
For Example:
class A
{
A(){
System.out.println(“From Constructor”);}
{
System.out.println(“From IIB”);}
public static void main(String[] args){
A a= new A();
} }
Output: From IIB
From Constructor
SIB: SIB is same as IIB. It stores all static members together. It is writen as static with curly bracket. SIB runs only once. This runs before main method and does not require any invoking statement. If no object is created, then also it will execute. From SBI and main, SBI have high priority.
For Examlpe:
class A{
static{
System.out.println(“From SIB”);
}
public static void main (String[] args){
System.out.println(“From Main”);
}
}
Output:
From SIB
From Main