Tuesday, 3 November 2015

static blank final variable

static blank final variable

A static final variable that is not initialized at the time of declaration is known as static blank final variable. It can be initialized only in static block.


/**
 * static blank final variable must be initialized in static block.
 * Otherwise there will compile time error.
 */
public class Solution {

      //static blank final variable
      static final int staticFinal;
      public Solution() {
          // The final field Solution.staticFinal cannot be assigned
          // staticFinal=50;
      }

      static {
          staticFinal=50;
      } 
      public static void main(String args[]){ 
          System.out.println("static final blank:"
                            +Solution.staticFinal); 
      }
}

Output: static final blank:50



If you will assign in the constructor, there will be error “The final field staticFinal cannot be assigned” at compile time.

No comments:

Post a Comment

Related Posts Plugin for WordPress, Blogger...