Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Negated rule; no longer intrudes on territory of DCL02-J

The final keyword is used to identify constant values. That is, final indicates fields that may not be changed during an invocation of a program.

The value of public final fields is permitted to be inserted inline into any compilation unit that reads the value. This means that if the field's value ever changes, then a compilation unit that depends on the value may still have the old value until re-compiled.

Another pitfall arises when static-final is used inappropriately to declare mutable data. See guideline OBJ01-J. Do not assume that declaring a reference to be final causes the referenced object to be immutable.

Noncompliant Code Example

In this noncompliant code example, class Foo stores a number representing the version of the software being used. It is subsequently accessed by class Bar, which lives in a separate compilation unit.

Foo.java:

Code Block
bgColor#ffcccc

class Foo {
  static public final int VERSION = 1;
  // ...
}

Bar.java:

Code Block

class Bar {
  public static void main(String[] args) {
    printf("You are using version " + Foo.VERSION);
  }
}

When compiled and run, the software correctly prints:

Code Block

    You are using version 1

However, a subtle flaw is possible in the future. When the version number must be upgraded, suppose a developer modifies Foo.java, and changes VERSION to have the value 2. The developer then recompiles Foo.java without recompiling Bar.java. Now the software incorrectly prints:

Code Block

    You are using version 2

because Bar.java still thinks that Foo.VERSION is 1.

While recompiling Bar.java will solve this problem, a better solution is available.

Compliant Solution

Wiki Markup

Literals that describe mathematical constants are often used to represent well established constants. This eliminates the need to use their actual values throughout the source code and reduces the possibility of committing frivolous errors. (See Guideline DCL02-J. Use meaningful symbolic constants to represent literal values in program logic for more information.)

If a mathematical constant is not declared static, every instance of the class object will needlessly retain its own copy of the constant. Moreover, failing to declare a constant field final can be counterproductive, as highlighted in guideline OBJ03-J. Do not use public static non-final variables. Disregarding this advice can expose the constants to pernicious thread safety issues.

Wiki Markup
At the same time, the use of {{static-final}} modifiers should not be abused. According to the Java Language Specification \[[JLS 2005|AA. Bibliography#JLS 05]\], Section 13.4.9, "{{final}} Fields and Constants" 

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.

That is, the JLS recommends

Thus a compliant solution would be:

Foo.java:

Code Block
bgColor#ffcccc

class Foo {
  static private final int version = 1;
  static public String getVersion
Code Block

private static int N;
public static int getN() {
    return Nversion;
  }

instead of

Code Block
public static final int N = // ...;
}

Another pitfall arises when static-final is used inappropriately to declare mutable data. (See guideline OBJ01-J. Do not assume that declaring a reference to be final causes the referenced object to be immutable.)

Noncompliant Code Example

This noncompliant code example does not qualify the constant value googol (10 raised to the power 100) with the static and final modifiers.

Code Block
bgColor#ffcccc

public BigDecimal googol = BigDecimal.TEN.pow(100); // mathematical constant

Compliant Solution

To be compliant, ensure that all mathematical constants are declared as static-final.

Code Block
bgColor#ccccff

public static final BigDecimal googol = BigDecimal.TEN.pow(100);

Bar.java:

Code Block

class Bar {
  public static void main(String[] args) {
    printf("You are using version " + Foo.getVersion());
  }
}

The private version value can therefore not be copied into the Bar class when it is compiled, thus preventing the bugNote that the variable googol is actually a static final reference to an object of type BigDecimal. Because instances of BigDecimal are immutable, guideline OBJ01-J. Do not assume that declaring a reference to be final causes the referenced object to be immutable is irrelevant in this case.

Exceptions

Wiki Markup
*DCL04-EX1*: According to the Java Language Specification \[[JLS 2005|AA. Bibliography#JLS 05]\], Section 9.3 "Field (Constant) Declarations," "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 may violate this guideline.

DCL04-EX3: Constants that never change their values throughout the lifetime of the software may indeed be declared final. For instance, the JLS recommends that mathematical constants be declared final.

Risk Assessment

Failing to declare mathematical constants static and final can lead to thread safety issues as well as to inconsistent behavior.

...