0

What is the use of using final keyword for local primitive variables in java. Is there any performance benefit ?

1
  • 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. Commented May 1, 2017 at 9:01

1 Answer 1

2

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."
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.