Open In App

Instance variable as final in Java

Improve
Improve
Like Article
Like
Save
Share
Report

Instance variable: As we all know that when the value of variable is varied from object to object then that type of variable is known as instance variable. The instance variable is declared inside a class but not within any method, constructor, block etc. If we don’t initialize an instance variable, then JVM automatically provide default value according to the data type of that instance variable.
But if we declare an instance variable as final, then we must have to take care about the behavior of instance variable.

  • There is no requirement as such to make the variable final. However, when it is true that you explicitly intend to never change the variable, it is often good practice to make it final
  • We have to use instance variable as final in the creation of immutable class.

Important points about final instance variable:

  1. Initialization of variable Mandatory : If the instance variable declared as final, then we have to perform initialization explicitly whether we are using it or not and JVM won’t provide any default value for the final instance variable.




    // Java program to illustrate the behavior 
    // of final instance variable
    class Test {
        final int x;
        public static void main(String[] args)
        {
            Test t = new Test();
            System.out.println(t.x);
        }
    }

    
    

    Output:

    error: variable x not initialized in the default constructor
    
  2. Initialization before constructor completion : For final instance variable we have to perform initialization before constructor completion. We can initialize a final instance variable at the time of declaration.




    // Java program to illustrate that 
    // final instance variable
    // should be declared at the 
    // time of declaration
    class Test {
        final int x = 10;
        public static void main(String[] args)
        {
            Test t = new Test();
            System.out.println(t.x);
        }
    }

    
    

    Output:

    10
    
  3. Initialize inside a non-static or instance block : We can also initialize a final instance variable inside a non-static or instance block also.




    // Java program to illustrate 
    // that final instance variable
    // can be initialize within instance block
    class Test {
        final int x;
        {
            x = 10;
        }
        public static void main(String[] args)
        {
            Test t = new Test();
            System.out.println(t.x);
        }
    }

    
    

    Output:

    10
    
  4. Initialization in default constructor : Inside default constructor we can also initialize a final instance variable.




    // Java program to illustrate that 
    // final instance variable
    // can be initialized 
    // in the default constructor
    class Test {
        final int x;
        Test()
        {
            x = 10;
        }
        public static void main(String[] args)
        {
            Test t = new Test();
            System.out.println(t.x);
        }
    }

    
    

    Output:

    10
    

The above mentioned are the only possible places to perform initialization for final instance variable. If we try to perform initialization anywhere else then we will get compile time error.




// Java program to illustrate 
// that we cant declare
// final instance variable 
// within any static blocks
class Test {
    final int x;
    public static void main(String[] args)
    {
        x = 10;
        Test t = new Test();
        System.out.println(t.x);
    }
}


Output:

error: non-static variable x cannot be referenced from a static context




// Java program to illustrate that we
//  cant declare or initialize
// final instance variable within any methods
class Test {
    final int x;
    public void m()
    {
        x = 10;
    }
}


Output:

error: cannot assign a value to final variable x


Last Updated : 21 Sep, 2017
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads