...
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" ]\] of the Java Language Specification (JLS 2005),
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
*DCL04-EX0*: According to \[[JLS 2005|AA. Bibliography#JLS 05]\], §9.3 "Field (Constant) Declarations" of the Java Language Specification [ (JLS 2005]), "Every field declaration in the body of an interface is implicitly {{ Wiki Markup 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.
DCL04-EX2: 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.
...