Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Migrated to Confluence 4.0

...

Although recompiling Bar.java solves this problem, a better solution is available.

Compliant Solution

Wiki MarkupAccording 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 and final. If the read-only nature of final is required, a better choice is to declare a private 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]]

[§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>

 

§9.3, "Field (Constant) Declarations"

 

§4.12.4, "final Variables"

 

§8.3.1.1, "static Fields"

...