Versions Compared

Key

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

...

That is, the JLS recommends this:

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

instead of:

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

Another pitfall arises when static-final is used inappropriately used to declare mutable data. (See guideline OBJ01-J. Be aware that a final reference may not always refer to immutable data.)

...

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."

...

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

...

Search for vulnerabilities resulting from the violation of this guideline on the CERT website.

Other Languages

Related Guidelines

This guideline appears in the C Secure Coding Standard as : DCL00-C. Const-qualify immutable objects.

Bibliography

Wiki Markup
\[[JLS 2005|AA. Bibliography#JLS 05]\] "13.4.9 final Fields and Constants", "9.3 Field (Constant) Declarations", "4.12.4 final Variables", "8.3.1.1 static Fields"

...