As a human being, we tend to do silly
mistakes. By following some good practices, we can avoid some of them. Making
local variables as final is one of the good practice to avoid some kind of
mistakes. Following points summarize some of the advantages of final local
variables.
1. Using final to local variables,
method parameters clearly express your intent.
Suppose you had a method like below.
public boolean createEmptyDocument(final
String fileName)
Any attempt to reassign a value to a
final variable fileName will result in a compiler error. So it guarantees that
you are creating empty document with same name.
2. JIT compiler can perform some
optimizations on final fields.
3.Declaring local variable as final
automatically ensures thread-safety for that field in multi threaded
environment.
You
may like
No comments:
Post a Comment