|
|
|
| |
final
Java keyword indicating that a variable, method
or class cannot be further defined or overridden
or subclassed. Final means, this value won't
be changed hereafter. The term final is used
in a number of contexts. static final variables
are close to constants in other languages, final
classes may not be subclassed, final methods
may not be overridden, private implies final.
Marking things final has two purposes: efficiency
and safety. The compiler can perform various
optimisations knowing the value cannot change.
Hotspot and optimising compilers now do this
anyway, whether or not you declare methods final,
so using final purely for efficiency is no longer
recommended. The compiler can also check to
ensure you do not inadvertently attempt to change
the value after computing its value once where
it is defined.
A little known feature of Java is blank finals.
You can declare member variables final, but
not declare a value. This forces all constructors
to initialise the blank final variables.
You can have both final instance and final static
variables, final statics are more common. When
you know the value of a constant at compile
time you might as well make it static. It takes
up less room, just one copy per class instead
of one copy per object. It is also faster to
access a static constant than an instance constant.
However, if you don't know the value of the
constant until instantiation time, you have
to make it an instance constant.
|
|