Non-static initializers are rarely used, and can be confusing for most developers because they only run when new class instances are created. When possible, non-static initializers should be refactored into standard constructors or field initializers. == Noncompliant Code Example [source,java] ---- class MyClass { private static final Map MY_MAP = new HashMap() { // Noncompliant - HashMap should be extended only to add behavior, not for initialization { put("a", "b"); } }; } ---- == Compliant Solution [source,java] ---- class MyClass { private static final Map MY_MAP = new HashMap(); static { MY_MAP.put("a", "b"); } } ---- or using Java 9 ``++Map.of++``: [source,java] ---- class MyClass { // Compliant private static final Map MY_MAP = java.util.Map.of("a", "b"); } ---- or using Guava: [source,java] ---- class MyClass { // Compliant private static final Map MY_MAP = ImmutableMap.of("a", "b"); } ---- ifdef::env-github,rspecator-view[] ''' == Implementation Specification (visible only on this page) include::message.adoc[] ''' == Comments And Links (visible only on this page) include::comments-and-links.adoc[] endif::env-github,rspecator-view[]