There is no good reason to have a mutable object as the public
(by default), static
member of an interface
.
Such variables should be moved into classes and their visibility lowered.
Similarly, mutable static
members of classes and enumerations which are accessed directly, rather than through getters and setters,
should be protected to the degree possible. That can be done by reducing visibility or making the field final
if appropriate.
Note that making a mutable field, such as an array, final
will keep the variable from being reassigned, but doing so has no effect on
the mutability of the internal state of the array (i.e. it doesn’t accomplish the goal).
This rule raises issues for public static
array, Collection
, Date
, and awt.Point
members.
Noncompliant code example
public interface MyInterface {
public static String [] strings; // Noncompliant
}
public class A {
public static String [] strings1 = {"first","second"}; // Noncompliant
public static String [] strings2 = {"first","second"}; // Noncompliant
public static List<String> strings3 = new ArrayList<>(); // Noncompliant
// ...
}