...
But if a developer changes the value of VERSION
to 2 by modifying Foo.java
and subsequently recompiles Foo.java
but fails to recompile Bar.java
, the software incorrectly prints:
Code Block |
---|
You are using version 1 |
...
According to §13.4.9, "final
Fields and Constants," of the Java Language Specification [JLS 2011],
...
Consequently, a compliant solution is as follows:
Foo.java
:
Code Block | ||
---|---|---|
| ||
class Foo { private static int version = 1; public static final String getVersion() { return version; } // ... } |
...
According to §9.3, "Field (Constant) Declarations," of the Java Language Specification [JLS 2011], "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."
Constants declared using the enum
type are permitted to violate this guideline.
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
.
Bibliography
[JLS 2011] | §13§4.12.4.9, " |
|
| §4§13.124.49, " |
...