Tuesday, April 16, 2013

Blank final variable and static blank final variable


Blank final variable:-

The final variable that is not initialized during the time of declaration is called blank final variable. These variables need to be initialized either in intializer block or in Constructor.

Static blank final variable:-

The static final variable that is not initialized during the time of declaration. These variables need to be initialized in static initializer block. Static initializer block is the only place where a static blank final variable can be initialized.

public class Test {

       static final int varStatic;
       final int var1;
       final int var2;
       static
       {
              System.out.println("In static initializer block");
              varStatic = 0;
       }
       //Initializer block
       {
              System.out.println("In initializer block");
              var1 = 0;
       }
       Test()
       {
              System.out.println("In constructor");
              var2 = 0;
       }
       public static void main(String[] args)
       {
              System.out.println("In Main");
       }

}



No comments:

Post a Comment