As value of this final variable wont change, JVM will cache the value of this variable to improve performance. A final variable is named blank final variable if it’s not initialized whereas declaration. – Ankur Shanbhag Aug 14 '13 at 7:32. Other methods in the class aren’t even aware that the variable exists. Keyword final is used along with: Variables; Methods; Classes final variables: If you use a variable as a final then the value of variable is become constant. why will you need that? There are many references suggesting a liberal use of final.The Java Language Specification even has a section on final variables.Various rules in static analysis tools also support this - PMD even has a number of rules to detect when final can be used.The pages that I linked to provide a number of points as to what final does and why you should use it liberally. Then you can use the variable only within that method. Final allows the Java compiler to make some optimization to improve the performance of the code. Non-final local variables can't be. Here's @Jon Skeet Is it correct to assume that these variable live beyond their local scope since they can be accessed by anonymous inner classes ? That means, the compiler makes sure that you can do it only once. The keyword final is a reserved keyword in Java to restrict the user and it can be applied to member variables, methods, class and local variables. Whether that's more or less useful to you than thinking of the original variable living beyond its local scope is your call :)@JonSkeet Thanks for the clear explanation :) I like to understand what's happening behind the scene when coding.This is the only answer i found to be something reasonably appropriate.. good one@KrushnaSure, but any compiler worth its salt will recognize that the variable never changes inside the method, and treat it exactly the same as if it had been declared final. A reference variable declared final can never be reassigned to refer to a different object. If the compiler knows that a method cannot be subclassed, it will certainly not look in the subclasses for the matching method when a method is called.

You cannot change the value of a final variable once it is initialized. I was wondering is there any usability of using final local variables.

Free 30 Day Trial We use cookies to ensure you have the best browsing experience on our website. A Java final variable will only initialize once, either via an initializer or an assignment statement. The java final keyword can be used in various context: site design / logo © 2020 Stack Exchange Inc; user contributions licensed under you can use it hard coded or have a private final in the classI also feel it helps in performing optimization. What are final variables in Java? By using our site, you Private self-hosted questions and answers for your enterpriseProgramming and related technical career opportunities Featured on Meta @Ankur that hasn't been the case for a long time. To declare a method as final, place the modifier as follows: public final void hra() Since local variables live on the stack and exist only for lifetime of the method but Inner class object may live longer so inner class can't access any non final local variable.

The compiler can very easily work out if a variable is changed or not and optimise accordingly. If you want to use your local variable inside inner calss, you have to make It tells the other programmers that whoever wrote it knew that the value of (12 answers) By using our site, you acknowledge that you have read and understand our Don’t stop learning now. Perfect answer Jon Skeet but there is another (little) benefit of final variables.The compiler will ensure the final variable is set once and only once.Let's say you have to initialize a variable and the computation of the value is complex (multiple if-then-else within if-then-else).

The only advantages are: someone reading or modifying the code will know the author's intent, and it prevents you from accidentally re-assigning the value.variables or reference types marked with final should and must initialize before compilation or else compilation fails. Get hold of all the important DSA concepts with the For example a simple code as belowThe example is quite simple one and may not be a relevant code but the question is more generic.I have seen a lot of codes(all incorporated in main function which have final local variables) Is there any usability of declaring local variables as final other than that they cannot be edited in the same function itself? That's the primary use of Note that as a matter of style, some people like to use final local variables may be accessed from anonymous inner subclasses, whereas non final local variables may not.Yes, the usability is :- local final variable are accessible by the method inner class. Of course, this is a bug. Variables are not overridden anyway when inheritance comes into picture. final fields, parameters, and local variables are read-only(means the object's identity, not its state). @Ahmed: Well behind the scenes, the compiler generates an instance variable in the anonymous inner class and copies the value of the original variable when the instance is created. final variables in Java 02-11-2010 In Java, when final keyword is used with a variable of primitive data types (int, float, .. etc), value of the variable cannot be changed.