The final
modifier indicates that a data field cannot be modified, as
in
final
double PI = 3.14;
Any attempt to assigne PI
to a new value will result in a runtime error. Thus final
makes data useful as constants.
Often such constants are needed by other classes, so they are
declared static
as well, as in
public
class MyMath
{
public final static double TWO_PI =
6.28;
...
}
Then other classes can then reference it, as in
...
double y = theta / MyMath.TWO_PI;
...
final
Methods
The final
modifier is also used with methods to indicate that they cannot
be overriden by subclasses.
public
class MyMath
{
...
public final double MyFormula() {
...
}
}
This helps to improve their performance since the JVM will
not need to check for overriding versions with the method is
invoked.
Latest update: Oct. 22, 2004