Open In App

Final Local Variables in Java

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

In Java, a local variable is a variable, which is declared inside a method. Local variables are only accessible within the method in which they are declared, other methods in the class do not know anything about that variable. When we declare a local variable, we need to initialize it first before using it.

Now, we have discussed what is local variable is, there is a similar concept, like this, which is a final local variable, and now we are going to discuss it in detail.

What is Final Local Variable?

A final local variable is a variable whose value can not be changed once it is assigned, it simply means we can not change its value throughout the program, the value remains constant.

Key features of final local variables:

  • Once a value is assigned then it can not be changed.
  • We can prevent unnecessary changes to the variable value.
  • A final local variable must be initialized before it is used.
  • The compiler executes the code faster because it already knows the value won’t change.

Example:

Java
// Demonstrating the working 
// of final local variable
public class Geeks{
    
    public static void main(String[] args) {
        
        // Declare and initialize a final local variable
        final int n= 5;
        
        // Try to change the value of number (this will cause an error)
        
        // this line will cause a compile-time error
        // number = 10;
        
        // Use the final variable
        System.out.println("The value of number is: " + n);
    }
}

Output
The value of number is: 5


Usability of final local variables:

  • Most importantly, we can use local variable as final in an anonymous inner class, we have to declare the local variable of anonymous inner class as final. This is to do with the individual accessor methods that get generated to implement the anonymous inner class. Non-final local variables can’t be used for inner classes.
  • It may allow Java compiler or Just In Time compiler to optimize code, knowing that the variable value will not change. This can improve the processing time of the program.


Important Points About Local Final Variable

1. Initialization of the Variable is not Mandatory

Even though local variable is final we have to perform initialization only if we want to use it. If we are not using, then it is not required to perform initialization even though it is final.

Example:

Java
// Java program to demonstrates the
// working of final local variable
class Geeks {
    public static void main(String[] args)
    {
        final int x;
        System.out.println("GEEKS");
    }
}

Output
GEEKS


2. Final is the Only Applicable Modifier for Local Variables

The only applicable modifier for local variable is final. By mistake if we trying to apply any other modifier then we will get compile time error.

Example:

Java
// Java program to demonstrates that final is
// only applicable modifier for local variable
class Geeks {
    public static void main(String[] args)
    {
        // static int x will also not work
        public int x; 
        System.out.println("GEEKS");
    }
}

Output:

Output


Article Tags :
Practice Tags :

Similar Reads