...
Although recompiling Bar.java
solves this problem, a better solution is available.
Compliant Solution
Wiki Markup |
---|
According to [§13.4.9|http://java.sun.com/docs/books/jls/third_edition/html/binaryComp.html#13.4.9 |
] "{{final}} Fields and Constants" of the _Java Language Specification |
_ \[[JLS |
2005|AA. Bibliography#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.
...
As a result, the private version value cannot be copied into the Bar
class when it is compiled, consequently preventing the bug. Note that most JIT code generators are capable of inlining the getVersion()
method at runtime; consequently there is little or no performance penalty incurred.
Exceptions
Wiki Markup |
---|
*DCL04-EX0*: According to [§9.3|http://java.sun.com/docs/books/jls/third_edition/html/interfaces.html#9.3] "Field (Constant) Declarations" of the _Java Language Specification |
_ \[[JLS |
2005|AA. Bibliography#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-EX1: Constants declared using the enum
type are permitted to violate this guideline.
...