The final
keyword can be used to specify constant values (that is, values that cannot change during program execution). However, constants that can change over the lifetime of a program should not be declared public final. The Java Language Specification [JLS 2011] allows implementations to insert the values of public final fields inline in any compilation unit that reads the field. Consequently, if the declaring class is edited so that the new version gives a different value for the field, compilation units that read the public final field could still see the old value until they are recompiled.
...
According to §13.4.9, "final
Fields and Constants," of the Java Language Specification [JLS 2011],
Other than for true mathematical constants, we recommend that source code make very sparing use of class variables that are declared
static
andfinal
. If the read-only nature offinal
is required, a better choice is to declare aprivate static
variable and a suitable accessor method to get its value.
...
According to §9.3, "Field (Constant) Declarations," of the Java Language Specification [JLS 2011], "Every field declaration in the body of an interface is implicitly public
, static
, and final
. It is permitted to redundantly specify any or all of these modifiers for such fields."
...
Constants whose value never changes throughout the entire lifetime of the software may be declared as final
. For instance, the Java Language Specification recommends that mathematical constants be declared final
.
Bibliography
[JLS 2011] | §4.12.4, " |
...