What is the use of using final keyword for local primitive variables in java. Is there any performance benefit ?
- I don't think there is a performance benefit. Except you use a local variable or parameter inside an anonymous class defined inside a method (in this case you must define it final), it's no too useful.Manuel Schmidt– Manuel Schmidt2017-05-01 09:01:55 +00:00Commented May 1, 2017 at 9:01
Add a comment |
1 Answer
The final keyword has more than one meaning:
- a final class cannot be extended
- a final method cannot be overridden
- final fields, parameters, and local variables cannot change their value once set
Using final:
- clearly communicates your intent.
- allows the compiler and virtual machine to perform minor optimizations
- clearly flags items which are simpler in behaviour - final says, "If you are looking for complexity, you won't find it here."