STATIC INITIALIZATION BLOCKS(SIB'S)

Static Initialization Block (SIB)


  • A Block named as Static inside a class is called Static Initialization Block(SIB).
  • SIB’s are invoked only once at the time of the corresponding loading class or reffered in to the memory.

Example 1:

Before compilation

 
						
                    class SibDemo
                    {
                       static
                       
                       {
                          System.out.println("start");
                       }
                       public staticvoid main(String[] args)
                       {
                          System.out.println("end");
                       }
                    } 
                        

After compilation

 
						
                    class SibDemo
                    {
                       static
                       {
                          System.out.println("start");
                       }
                       SibDemo  ( )
                       {
                          super();
                          /*  initialization of instance attributes  */
                          /*  call for IIB’s  */
                          /*  body of constructor  */
                       }
                       public static void main(String[] args)
                       {
                          System.out.println("end");
                       }
                    } 
                        

   Output:

start end

Example 2:

Before compilation

 
						
                    public class SibDemo
                    {
                       static
                       
                       {
                          System.out.println("SIB-I");
                       }
                       static
                       
                       {
                          System.out.println("SIB-II");
                       }
                       {
                          System.out.println("IIB-I");
                       }    /*   IIB   */
                       {
                          System.out.println("IIB-II");
                       }      /*   IIB   */
                       public static void main(String[] args)
                       {
                          SibDemo s=new SibDemo();
                       }
                    } 
                        

After compilation

 
						
                    public class SibDemo
                    {
                       static
                       
                       {
                          System.out.println("SIB-I");
                       }
                       static
                       
                       {
                          System.out.println("SIB-II");
                       }
                       {
                          System.out.println("IIB-I");
                       }
                       {
                          System.out.println("IIB-II");
                       }
                       SibDemo  ( )
                       {
                          super();
                          /*  initialization of non static attributes  */
                          /*  call for IIB’s  */
                          /*  body of constructor  */
                       }
                       public static void main(String[] args)
                       {
                          SibDemo s=new SibDemo ();
                       }
                    } 
                        

   Output:

SIB-I SIB-II IIB-I IIB-II

Explanation :

  • jre will load .class file first into memory in execution.
  • Check for static blocks. So SIB-1,SIB-2.
  • And then thread to main method.
  • ‘Sibs’ class has no extend to implicitly but by default the class is “object class”.
  • Static blocks will execute only once i.e., whenever particular class refers into the memory at the time of execution.
  • IIB’s are invoked for every object creation after execution of the super() but not after execution of the this().