I didn’t find anything on this topic, but this is bugging me a bit. Since Java 9 the java.util package provides immutable static factory methods for collections. But to me it seems that these are not (completely?) integrated in the following rules
- Mutable fields should not be “public static”
- Mutable members should not be stored or returned directly
For example, it raises the first warning with
public static final Set<String> SOME_WORDS = Set.of(“some”, “words”);
but not with
public static final Set<String> SOME_WORDS = Collections.unmodifiableSet(Set.of(“some”, “words”));
even though Set.of already provides an immutable set.
I think these ‘new’ factory methods should be taken into account with these rules.