Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

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
bgColor#ccccff
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

 

...

Image Modified