wellnwill Marathahalli Bangalore

Blocks


  • A block is a group of one or more statements defined in curly braces.
  • Block does not have name so we cannot call the block explicitly.
  • Blocks will be invoked by JVM automatically.
  • The blocks defined inside the class directly known as initialization block.

Types of initialization blocks

There are two types of initialization blocks in java:

  • Static initialization block
  • Instance initialization block (non-static block)

Static initialization block

If we have to perform one time activity at the time of class loading then we will use static initialization block.

Example- The corresponding native libraries should be loaded at the time of class loading that is why these statements should be written inside static block.

  • Static blocks are executed only once at the time of class loading. Class loading will happen only once when we or JVM uses the class first time.
  • It is represented as-
static
{
}
  • Static block is also used for initializing the static variable dynamically.
  • We can assign static variable from instance block also but this is not good approach.
  • Static blocks are executed before main method.
  • We can write number of static blocks as per program requirement and these blocks will be executed from top to bottom.
  • Instance variable cannot be accessed from static context directly but it can be accessed with object reference.
  • If there are multiple static blocks then all static blocks will be executed in the order of entries in the class.
class Test169
{
    static int a=3;
    static int b;
    static void m(int x)
    {
      System.out.println("x = " + x);
      System.out.println("a = " + a);
      System.out.println("b = " + b);
    }
   static 
   {
     System.out.println("Static block initialized.");
     b = a * 4;
   }
   public static void main(String args[]) 
   {
     m(42);
   }
}

// Output - Static block initialized x=42 a=3 b=12
class Test0169
{
   int i;
   static int a=3;
   static int b;

   static 
   {
      Test0169 t1=new Test0169();
      System.out.println(t1.i);
      System.out.println("Static block initialized.");
      b = a * 4;
      System.out.println("b:"+b);
   }
   public static void main(String args[]) 
   {
   }
}

// Output-0, Static block initialized,b:12



Static control flow

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

  • Identification of static members from top to bottom. If it is static variable then JVM will assign the default value at the time of identification.
  • Execution of static variable assignment and static blocks from top to bottom.
  • Execution of main method
class Test170
{  
   static
   {
     test();
     System.out.println("1st static block");
    }
   static void test()
    {
      System.out.println("a :"+a);
      System.out.println("inside test()");
    }
    
    public static void main(String args[])
    {	
      test();
      System.out.println("inside main()");
    }
     static int a=2;
     static
    {
     System.out.println("a :"+a);
     System.out.println("2nd static block");
    }
} 

// Output-a :0, inside test(), 1st static block, a :2, 2nd static block,
a :2, inside test(), inside main().

Important points

What is meaning of direct read and indirect read? Direct Read: If we are trying to read a static variable from static block, it is known as direct read. Indirect Read: If we are trying to read a static variable from method, it is known as indirect read

class Test171
{  
   static int a=2;
    static
    {
     test();
     System.out.println("a :"+a);//direct read
    }
    static void test()
    {
     System.out.println("a :"+a);//indirect read
    }
    public static void main(String args[]){	
    System.out.println("inside main()");
    }
} 

// Output - a :2, a :2 ,inside main()

Before assigning actual value to static variable if we perform direct read then we will get compile time error i.e. illegal forward reference

class Test172
{  
   static
    {
     System.out.println("a :"+a);//direct read without actual value
    }
      static int a=2;
      public static void main(String args[])
    {	
      System.out.println("inside main()");
    }
} 

// Output - CompileTimeException - illegal forward reference	

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

  • Identification of static members from parent to child.
  • Execution of static variable assignment and static block from parent to child.
  • Execution of only child class main method.

Note: When “child class” class loading will be performed automatically parent class will be loaded but whenever we are loading parent class, child class will not be loaded because parent class member by default available in child class but child class member will not be available in parent class.

Footer with Map