Non-static initializers are rarely used, and can be confusing for most developers. When possible, they should be refactored into standard constructors or field initializers.
The following code:
class MyClass {
private static final Map MY_MAP = new HashMap() {
// Non-Compliant - HashMap should be extended only to add behavior, not for initialization
{
put("a", "b");
}
};
}
could be refactored using Guava into:
class MyClass {
// Compliant
private static final Map MY_MAP = ImmutableMap.of("a", "b");
}