...
Wiki Markup |
---|
At the same time, the use of {{static-final}} modifiers should not be abused. According to the Java Language Specification \[[JLS 05|AA. Java References#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
andfinal
. If the read-only nature offinal
is required, a better choice is to declare aprivate static
variable and a suitable accessor method to get its value.
...
Noncompliant Code Example
This noncompliant code snippet example does not qualify the constant value googol (10 raised to the power 100) with the static
and final
modifiers.
...
Code Block | ||
---|---|---|
| ||
private static final BigDecimal googol = BigDecimal.TEN.pow(100); public static BigDecimal getGoogol() { return googol; } |
Exceptions
Wiki Markup |
---|
*DCL31-J:EX1*: According to the Java Language Specification \[[JLS 05|AA. Java References#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." |
Risk Assessment
Failing to declare mathematical constants static
and final
can lead to thread safety issues as well as inconsistent behavior.
...