wellnwill Marathahalli Bangalore

Instance initialization block (non-static block)


  • The functionality of constructor is to initialize the instance variables of the class. Other than initialization if we want to perform any activity for every object creation then we should go for instance block.
  • Instance initialization block will be executed each time when object will be created.
  • It is represented as-
        {

        }
	Instance initialization block will be executed before constructor.
        Ex- incrementing count value for every object creation etc.

Instance control flow

The following sequence of steps will be executed as a part of instance control flow.

  • Identification of instance member from top to bottom at the time of object creation and JVM will assign the default value at the time of identification.
  • Execution of instance variable assignments and instance blocks from top to bottom.
  • Execution of constructor.

Note : Without object creation, non static control flow will not be performed.

Remember

Static control flow will be performed at the time of class loading and it is one time activity. Instance control flow will be performed for every object creation. It is not one time activity.


class Test173
{  
    static
     {
       test();
       System.out.println("1st init block");
     }
	
    Test173()
    {
       System.out.println("constructor");
    }
	
   void test()
    {
       System.out.println("a :"+a);
       System.out.println("inside test()");
    }
	
   public static void main(String args[])
   {	
     Test173 t1=new Test173(); 
     System.out.println("inside main()");
   }
   
    int a=2;
    {
       System.out.println("a :"+a);
       System.out.println("2nd init block");
    }
} 

// Output - a :0, inside test(), 1st init block, a :2, 2nd instance block, constructor, inside main()
class Test0171
{  
    static
    {
       Test0171 t1=new Test0171();
       System.out.println("static block");
    }
	
    {
       System.out.println("non static block");
    }

    Test0171()
    {
       System.out.println("constructor");
    }
    public static void main(String args[])
    {	
      System.out.println("inside main()");
    }
}

Output-non static block
       Constructor
       Static block
       Inside main()


	   
	   
	   
	   
	   

Instance control flow in parent class to child class relationship (when we are executing child class)

  • Identification of instance member from parent class to child class.
  • Execution of instance variable assignments and instance block from top to bottom only in parent class.
  • Execution of parent class constructor .
  • Execution of instance variable assignments and instance block from from top to bottom only in child class.
  • Execution of child class constructor

class Bulb
{
   int watt;
   float price;
   String color;
   boolean state;
   static String cname;
   static int count;
 
   static
   {        
     count=0;
     cname="Bosch";
   }
 
  {
    count++;
  }
 
  Bulb(int w, float p, String clr, boolean b)
  {
    watt=w;
    price=p;
    color=clr;
    state=b;
  }
}  
  
class Buld
{
  static void countBulb()
  {
     System.out.println("Total number of Bulbs :"+Bulb.count);
  }
  void displayDetails()
  {
     System.out.println("watt: "+watt);
     System.out.println("price: "+price);
     System.out.println("color: "+color);
     System.out.println("state: "+state);
     System.out.println("company: "+cname);
  }
}
class Test160
{
  public static void main(String args[])
  {
     Bulb.countBulb();
     Bulb b1=new Bulb(120,25f,"red",true);
     Bulb b2=new Bulb(150,35f,"orange",false);
     Bulb.countBulb();
     System.out.println("-----Details of Bulb b1-----");
     b1.displayDetails();
     System.out.println("-----Details of Bulb b2-----");
     b2.displayDetails(); 
  }
}

Problem without using static with counter variable


class Bulb
{
   int watt;
   float price;
   String color;
   boolean state;
   static String cname;
   int count;
 
   static
   {        
      cname="Bosch";
   }
 
  {
     count++;
  }
 
  Bulb(int w, float p, String clr, boolean b)
  {
      watt=w;
      price=p;
      color=clr;
      state=b;
  }
  
  void countBulb()
  {
    System.out.println("Total number of Bulbs :"+count);
  }
class Test160
{
 void displayDetails()
 {
    System.out.println("watt: "+watt);
    System.out.println("price: "+price);
    System.out.println("color: "+color);
    System.out.println("state: "+state);
    System.out.println("company: "+cname);
 }
}

class Test161
{
   public static void main(String args[])
   {
      Bulb b1=new Bulb(120,25f,"red",true);
      Bulb b2=new Bulb(150,35f,"orange",false);
      b1.countBulb();
      b2.countBulb();
      System.out.println("-----Details of Bulb b1-----");
      b1.displayDetails();
      System.out.println("-----Details of Bulb b2-----");
      b2.displayDetails();
   }
}





  • If there are multiple static blocks then all static blocks will be executed in the order of entries in the class.
  • In the case of multiple Instance initialization blocks, all Instance initialization blocks will be executed in the order of entries in the class.

class Bulb
{  
    float price; 
    static
    {
       System.out.println("static block 1 is invoked");
    }
    static
    {
       System.out.println("static block 2 is invoked");
    }
    	
    Bulb()
    {
	System.out.println("constructor is invoked");
    }  
   
    {
	System.out.println("instance block 1 is invoked");
    } 

    {
	System.out.println("instance block 2 is invoked");
    } 
} 	
class Test162
{ 	 
    public static void main(String args[])
    {  
        Bulb b1=new Bulb();  
        Bulb b2=new Bulb();  
    }
}



Footer with Map

Top