Use final liberally. Please do.


Use the final keyword liberally to communicate your intent. 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

In the last case, “value” for primitives is understood in the usual sense, while “value” for objects means the object’s identity, not its state. Once the identity of a final object reference is set, it can still change its state, but not its identity. Declaring primitive fields as final automatically ensures thread-safety for that field.

Some habitually declare parameters as final, since this almost always is the desired behaviour. Others find this verbose, and of little real benefit.

Consistently using final with local variables (when appropriate) can be useful as well. It brings attention to the non-final local variables, which usually have more logic associated with them (for example, result variables, accumulators, loop variables). Many find this verbose. A reasonable approach is to use final for local variables only if there is at least one non-final local variable in the method ; this serves to quickly distinguish the non-final local variables from the others.

Using final :

  • clearly communicates your intent
  • allows the compiler and virtual machine to perform minor optimizations
  • clearly flags items which are simpler in behavior - final says, “If you are looking for complexity, you won’t find it here.”


Similar Articles

Linked data based integration
New Date and Time API in Java8
Re-platformig this blog to Wintersmith
Improving code quality in agile teams
Java7 updates

blog comments powered by Disqus