...
Although recompiling Bar.java
solves this problem, a better solution is available.
Compliant Solution
According to [§13.4.9, "{{ Wiki Markup 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.
...
In 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 can inline the getVersion()
method at runtime, so little or no performance penalty is incurred.
Exceptions
...
*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-EX2: Constants declared using the enum
type are permitted to violate this guideline.
...
C Secure Coding Standard: DCL00-C. Const-qualify immutable objects
Bibliography
...
[[JLS 2005AA. References#JLS 05]] | http://java.sun.com/docs/books/jls/third_edition/html/binaryComp.html#13.4.9] | ]]></ac:plain-text-body></ac:structured-macro> | |
| |||
| |||
|
...