SIB,IIB and Examples on SIB and IIB

SIB – Static Initialization Block
SIB executes when the class gets loaded and executes only once in entire execution

IIB – Instance Initialization Block
IIB 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. package com.kb.instanceblock;
  2.  
  3. public class SIB1 {
  4.    
  5.     static {
  6.         System.out.println("SIB");
  7.     }
  8.    
  9.    
  10.     {
  11.         System.out.println("IIB");
  12.     }
  13.    
  14.     public SIB1() {
  15.         System.out.println("Constructor");
  16.     }
  17.  
  18.     public static void main(String[] args) {
  19.        
  20. SIB1 sib = new SIB1();
  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();
	}

}


So when the class gets loaded,
SIB is called whenever constructor gets called,IIB executes first and then constructor block executes

So always the sequence of execution is
1) SIB
2) IIB
3) constructor

Let’s see another example

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

	}

}
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.

Let’s see with example

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

	}

}

About the Author

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