MutablePublicArray (original) (raw)
MutablePublicArray
Non-empty arrays are mutable, so this `public static final` array is not a constant and can be modified by clients of this class. Prefer an ImmutableList, or provide an accessor method that returns a defensive copy.
The problem
Nonzero-length arrays are mutable. Declaring one public static final indicates that the developer expects it to be a constant, which is not the case. Making itpublic is especially dangerous since clients of this code can modify the contents of the array.
There are two ways to fix this problem:
- Refactor the array to an
ImmutableList. - Make the array
privateand add apublicmethod that returns a copy of theprivatearray.
See Effective Java 3rd Edition ยง15 for more details.
Suppression
Suppress false positives by adding the suppression annotation @SuppressWarnings("MutablePublicArray") to the enclosing element.