Open In App

Instance Variable as Final in Java

Last Updated : 25 Apr, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

As we all know, when the value of a variable varies from object to object, then that type of variable is known as an instance variable. The instance variable is declared inside a class but not within any method, constructor, or block.

If we don’t initialize an instance variable, then the JVM automatically provides a default value according to the data type of that instance variable. But if we declare an instance variable as final, then we must take care of its initialization behavior.

Why Use Final Instance Variables?

Using final ensures that the variable’s value cannot be changed once it is assigned. There is no strict requirement to make the variable final. When it is clear that you explicitly intend never to change the variable, it is considered good practice to make it final. Sometimes we use final instance variables in the creation of immutable classes.

Important Points About Final Instance Variables

1. Initialization is Mandatory

If the instance variable is declared as final, then we must perform initialization explicitly. The JVM does not provide any default value for final instance variables.

Example: Missing Initialization

Java
// Java program to illustrate the behavior
// of final instance variable
class Test {
    final int x;  // Not initialized

    public static void main(String[] args) {
        Test t = new Test();
        System.out.println(t.x); // Compilation error
    }
}

Output:

error: variable x not initialized in the default constructor


2. Initialization at Declaration

We can initialize a final instance variable at the time of declaration.

Example:

Java
// Java program to illustrate that  
// final instance variable can be initialized at 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. Initialization Inside an Instance Block

We can also initialize a final instance variable inside a non-static or instance block.

Example:

Java
// Java program to illustrate that  
// final instance variable can be 
// initialized in an 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 Inside the Constructor

We can initialize a final instance variable inside a constructor.

Example:

Java
// Java program to illustrate that  
// final instance variable can be initialized in 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


Invalid Ways to Initialize Final Instance Variables

1. Cannot Be Initialized in a Static Block

Since instance variables belong to objects and static blocks run before object creation, it is not possible to initialize a final instance variable in a static block.

Example:

Java
// Java program to illustrate that we can't initialize  
// final instance variable in a static block
class Test {
    final int x;

    public static void main(String[] args) {
        
        // Cannot assign to non-static 
        // final field from static context
        x = 10;  
        Test t = new Test();
        System.out.println(t.x);
    }
}

Output:

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


2. Cannot Initialize in Method

Final instance variables cannot be assigned inside instance methods after object construction, because they must be assigned before the constructor finishes.

Example:

Java
// Java program to illustrate that we can't initialize  
// final instance variable in a method
class Test {
    final int x;

    public void m() {
        x = 10; 
    }
}

Output:

error: cannot assign a value to final variable x


Article Tags :
Practice Tags :

Similar Reads