Tuesday, 22 November 2016

Initializing a blank final variable in Java


How to initialize a blank final variable in Java?


Example Java Program to demonstrate the initialization of blank final variable using Constructor

                  
                  A field can be declared as final. Doing so prevents its contents from being modified, A final variable that is not initialized at the time of declaration is known as blank final variable. The blank final variable can be only initialized using its constructor method.

                   
                  As per the coding convention, it is common to use all upper case letters to represent a final variable. 


Example: 

  
                    final in REGNO = 15480;

Progam:


//Java Program to demonstrate the initialization of blank final variable using Constructor
class FinalInitialize
{
     final int VALUE;
     FinalInitialize(){       // Constructor
          VALUE=10;           // Initialized final variable inside constructor
          System.out.println("Final Value is : "+VALUE);
     }
}
public class InitializeFinal {
     public static void main(String[] args) {
          new FinalInitialize();
     }
}

               In the above program, The FinalInitialize() constructor initiate the value of the final variable VALUE.

No comments :

Post a comment