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 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 re-compiledrecompiled.
A related error can arise when a programmer declares a static final
reference to a mutable object; see guideline "VOID OBJ02-J. Never conflate immutability of a reference with that of the referenced object" for additional information.
...
Code Block |
---|
You are using version 1 |
However, But if a developer changes the value of VERSION
to 2 by modifying Foo.java
and recompiles Foo.java
, but fails to recompile Bar.java
, the software incorrectly prints:
...
Wiki Markup |
---|
According to [§13.4.9, "{{final}} Fields and Constants" |http://java.sun.com/docs/books/jls/third_edition/html/binaryComp.html#13.4.9] of the _Java Language Specification_ \[[JLS 2005|AA. References#JLS 05]\], |
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.
...
Code Block | ||
---|---|---|
| ||
class Bar { public static void main(String[] args) { System.out.println("You are using version " + Foo.getVersion()); } } |
As a resultIn this solution, the private version value cannot be copied into the Bar
class when it is compiled, consequently preventing the bug. Note that most just-in-time (JIT) code generators are capable of inlining can inline the getVersion()
method at runtime; consequently there is , so little or no performance penalty is incurred.
Exceptions
Wiki Markup |
---|
*DCL04-EX1*: According to [§9.3, "Field (Constant) Declarations" |http://java.sun.com/docs/books/jls/third_edition/html/interfaces.html#9.3] of the _Java Language Specification_ \[[JLS 2005|AA. References#JLS 05]\], "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." |
...
DCL04-EX3: 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
.
Risk Assessment
Failing to declare mathematical constants static
and final
can lead to thread-safety issues , as well as to inconsistent behavior.
...
C Secure Coding Standard: "DCL00-C. Const-qualify immutable objects"
Bibliography
<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="47840ec6a0b4b27b-eab1f229-42164d48-9b5fa4a4-5fbe3bf65d60676e83e1a62d"><ac:plain-text-body><![CDATA[ | [[JLS 2005 | AA. References#JLS 05]] | [§13.4.9, "final Fields and Constants" | http://java.sun.com/docs/books/jls/third_edition/html/binaryComp.html#13.4.9] | ]]></ac:plain-text-body></ac:structured-macro> |
| |||||
| |||||
|
...