Instance Initialization Block (IIB) IN JAVA

Instance Initialization Block (IIB)


  • A block without a name,inside the class is called as Instance Initialization Block(IIB).
  • IIB’s will be called automatically for every object creation after super() inside a constructor but not after this() function.
  • If we want to perform common task for all objects for a specific class we use IIB’s.

Example 1:

Before compilation

 
						
                    class Q
                    {
                       {
                          System.out.println("IIB-1");
                          /* Here this  statement is common task for all objects.*/
                       }
                    }
                    public class Iibs
                    {
                       public staticvoid main(String[ ] args)
                       {
                          Q a1=new Q();
                          Q a2=new Q();
                       }
                    } 
                        

After compilation

 
						
                    class Q extends Object
                    {
                       /* Object class is base class for all java classes.*/
                       {
                          System.out.println("IIB-1");
                       }
                       Q( )
                       {
                          /*  super( );  */
                          /*  Initialization of non static attributes  */
                          /*  Call for IIB’s  */
                          /*  Body of the constructor  */
                       }
                    }
                    public class IibDemo extends Object
                    {
                       IibDemo ()
                       {
                          /*  Super()  */
                          /*  initialization of instance attributes  */
                          /*  Call for IIB’s  */
                          /*  Body of the constructor  */
                       }
                       public static void main(String[ ] args)
                       {
                          Q a1=new Q();
                          Q a2=new Q();
                       }
                    } 
                        

   Output:

IIB-1 IIB-1

Example 2:

 
						
                     class Persn
                     {
                        Persn()
                        {
                           System.out.println("he is under SDJ Training");
                        }
                        Persn(int i)
                        {
                           System.out.println("he is under SDJ Training");
                        }
                        Persn(String str)
                        {
                           System.out.println("he is under SDJ Training");
                        }
                     }
                     public class Inbbs
                     {
                        public static void main(String[] args)
                        {
                           Persn ram=new Persn();
                           Persn ankit=new Persn(10);
                           Persn praveen=new Persn("jaipur");
                        }
                     }                        

   Output:

he is under SDJ Training he is under SDJ Training he is under SDJ Training

Explanation :

  • In the above program “he is under SDJ Training” is common task. But we are assigning this task inside every constructor which is waste of time.
  • Sun people came up with IIB’s for doing common task for all objects.
  • So IIB’s can reduce the time and can assign common task to all Objects of specific class.
  • We can keep number of IIB’s in one class.
  • For every Object creation IIB’s will be executed only once.n number of any type of constructors can be called after executing super();.

Modify the above program:

 
						
                     class Prsn
                     {
                        {
                           System.out.println("he is under SDJ training");
                        }
                        Prsn()
                        {
                           
                        }
                        Prsn(int i)
                        {
                           
                        }
                        Prsn(String str)
                        {
                           
                        }
                     }
                     public class Commoniib
                     {
                        public static void main(String[] args)
                        {
                           Prsn ram=new Prsn();
                           Prsn ravi=new Prsn(10);
                           Prsn jaipur=new Prsn("Praveen");
                        }
                     }                        
                       	

   Output:

he is under SDJ Training he is under SDJ Training he is under SDJ Training

After compilation

 
						
                    class Prsn
                    {
                       {
                          System.out.println("he is under SDJ training");
                       }
                       Prsn()
                       {
                          super();
                          /*  initialization of instance attributes   */
                          /*  Call for IIB’s.  */
                          /*  Body of constructor.  */
                       }
                       Prsn(int i)
                       {
                          super();
                          /*  initialization of instance attributes  */
                          /*  Call for IIB’s.  */
                          /*  Body of constructor.  */
                       }
                       Prsn(String str)
                       {
                          super();
                          /*  initialization of instance attributes  */
                          /*  Call for IIB’s.  */
                          /*  Body of constructor.  */
                       }
                    }
                    public class Commoniib
                    {
                       Commoniib( )
                       {
                          super( )
                          /*  initialization of instance attributes  */
                          /*  Call for IIB’s  */
                          /*  Body of constructor.  */
                       }
                       public static void main(String[] args)
                       {
                          Prsn ram=new Prsn();
                          Prsn ravi=new Prsn(10);
                          Prsn anvi=new Prsn("Praveen");
                       }
                    } 
                        

   Output:

he is under SDJ Training he is under SDJ Training he is under SDJ Training

Example 3:

 
						
                     class You
                     {
                        {
                           System.out.println("IIB-I-A");
                        }
                        {
                           System.out.println("IIB-II-A");
                        }
                        You()
                        {
                           
                        }
                        You(int i)
                        {
                           this();
                        }
                     }
                     class Me extends You
                     {
                        {
                           System.out.print("IIB-I-A");
                        }
                        {
                           System.out.print("IIB-II-B");
                        }
                        Me( )
                        {
                           this("SDJ");
                        }
                        Me(String str)
                        {
                           super(10);
                        }
                     };
                     
                     public class We
                     {
                        public static void main(String[] args)
                        {
                           You u1=new You();
                           You u2=new You(10);
                           Me  m1=new Me("SDJ");
                           Me  m2=new Me();
                        }
                     }                        

   Output:

IIB-I-A IIB-II-A IIB-I-A IIB-II-A IIB-I-A IIB-II-A IIB-II-B IIB-I-A IIB-II-A IIB-I-A IIB-II-B