SIB,IIB and Examples on SIB and IIB

SIB –Static Initialization Block
IIB – Instance Initialization Block

SIB : It executes when the class gets loaded and executes only once in entire execution

IIB : It executes when the constructor is called but before the execution of constructor. So it executes as many times as constructor gets executed.

Let’s see the example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
package com.kb.instanceblock;
 
public class SIB1 {
    
    static {
        System.out.println("SIB");
    }
    
    
    {
        System.out.println("IIB");
    }
    
    public SIB1() {
        System.out.println("Constructor");
    }
 
    public static void main(String[] args) {
        
SIB1 sib = new SIB1();
    }
 
}
package com.kb.instanceblock;

public class SIB1 {
	
	static {
		System.out.println("SIB");
	}
	
	
	{
		System.out.println("IIB");
	}
	
	public SIB1() {
		System.out.println("Constructor");
	}

	public static void main(String[] args) {
		
SIB1 sib = new SIB1();
	}

}

Output

so when the class gets loaded, SIB is called and whenever constructor gets called, IIB executes and then constructor block executes
So always the sequence of execution is
1)SIB 2)IIB 3)constructor

Lets see another example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
package com.kb.instanceblock;
 
public class SIB2 {
    
    static {
        System.out.println("SIB");
    }
    
    {
        System.out.println("IIB");
    }
    
    public SIB2() {
        System.out.println("Constructor");
    }
    
 
    public static void main(String[] args) {
        SIB2 sib1 = new SIB2();
        System.out.println("-------------");
        SIB2 sib2 = new SIB2();
 
    }
 
}
package com.kb.instanceblock;

public class SIB2 {
	
	static {
		System.out.println("SIB");
	}
	
	{
		System.out.println("IIB");
	}
	
	public SIB2() {
		System.out.println("Constructor");
	}
	

	public static void main(String[] args) {
		SIB2 sib1 = new SIB2();
		System.out.println("-------------");
		SIB2 sib2 = new SIB2();

	}

}

Output ?

Note:
SIB executed only once even though we created 2 objects but IIB executed 2 times

What if more than one SIB and IIB blocks are in the same class ?

All SIB blocks gets executes first and then all IIB blocks executes and finally constructor executes.

Lets see with example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
package com.kb.instanceblock;
 
public class SIB3 {
 
    
    static {
        System.out.println("SIB-1");
    }
    
    static {
        System.out.println("SIB-2");
    }
    
    {
        System.out.println("IIB-1");
    }
    
    {
        System.out.println("IIB-2");
    }
    public SIB3() {
        System.out.println("Constructor");
    }
    
 
    public static void main(String[] args) {
        SIB3 sib3 = new SIB3();
 
    }
 
}
package com.kb.instanceblock;

public class SIB3 {

	
	static {
		System.out.println("SIB-1");
	}
	
	static {
		System.out.println("SIB-2");
	}
	
	{
		System.out.println("IIB-1");
	}
	
	{
		System.out.println("IIB-2");
	}
	public SIB3() {
		System.out.println("Constructor");
	}
	

	public static void main(String[] args) {
		SIB3 sib3 = new SIB3();

	}

}

Output ?

About the Author

Karibasappa G C (KB)
Founder of javainsimpleway.com
I love Java and open source technologies and very much passionate about software development.
I like to share my knowledge with others especially on technology 🙂
I have given all the examples as simple as possible to understand for the beginners.
All the code posted on my blog is developed,compiled and tested in my development environment.
If you find any mistakes or bugs, Please drop an email to kb.knowledge.sharing@gmail.com

Connect with me on Facebook for more updates

Share this article on