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
- 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(); } }
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
- 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(); } }
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
- 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(); } }
Great brother! you make our life so easy. Thankyou so much
class IIB
{
//IIB
{
System.out.println(“This is IIB’s Priority”);
}
//SIB
static
{
System.out.println(“This is SIB’s Priority”);
}
//CONSTRUCTOR
IIB()
{
System.out.println(“And this is a constructor….”);
}
}
class test extends IIB
{
public static void main(String args[])
{
IIB b = new IIB();
}
}
Give this man a medal….great work on the website man!
Thank you so much John π
Sir where to use Non static block (IIB),, any real time example where we use Non Static Block,,, what is the need of NSB..?
As its Name implies Instance initialization block so we can initialize instance data member as whole in the block…Hope you find it useful
great explained.