Final local variables in Java - GeeksforGeeks

25 Sep, 2017
Prerequisite : final keyword, Variables, Scope of Variables
A local variable in Java is a variable that’s declared within the body of a method. Then you can use the variable only within that method. Other methods in the class aren’t even aware that the variable exists. If we are declaring a local variable then we should initialize it within the block before using it. In case of local variable, JVM won’t provide any default values.
A final local variable serves as a warning when you “accidentally” try to modify a value and also provides information to the compiler that can lead to better optimization of the class file.
Usability of using 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 :
Initialization of the variable is not Mandatory: Even though local variable is final we have to perform initialization only if you want to use it i.e. if we are not using then it is not required to perform initialization even though it is final.// Java program to illustrate the behavior of// final local variableclass Test {    public static void main(String[] args)    {        final int x;        System.out.println("GEEKS");    }}Output:GEEKS
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.// Java program to illustrate that final is// the only applicable modifier for local variableclass Test {    public static void main(String[] args)    {        public int x; // static int x will also not work.        System.out.println("GEEKS");    }}Output:error: illegal start of expression

Initialization of the variable is not Mandatory: Even though local variable is final we have to perform initialization only if you want to use it i.e. if we are not using then it is not required to perform initialization even though it is final.// Java program to illustrate the behavior of// final local variableclass Test {    public static void main(String[] args)    {        final int x;        System.out.println("GEEKS");    }}Output:GEEKS

// Java program to illustrate the behavior of// final local variableclass Test {    public static void main(String[] args)    {        final int x;        System.out.println("GEEKS");    }}
Output:
GEEKS

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.// Java program to illustrate that final is// the only applicable modifier for local variableclass Test {    public static void main(String[] args)    {        public int x; // static int x will also not work.        System.out.println("GEEKS");    }}Output:error: illegal start of expression

// Java program to illustrate that final is// the only applicable modifier for local variableclass Test {    public static void main(String[] args)    {        public int x; // static int x will also not work.        System.out.println("GEEKS");    }}
Output:
error: illegal start of expression

This article is contributed by Bishal Kumar Dubey. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Java-final keyword
Java
Java
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Functional Interfaces in Java
Stream In Java
Constructors in Java
Different ways of Reading a text file in Java
Exceptions in Java
Generics in Java
Comparator Interface in Java with Examples
Strings in Java
How to remove an element from ArrayList in Java?
Difference between Abstract Class and Interface in Java