Non-static initializers, also known as instance initializers, are blocks of code within a class that is executed when an instance of the class is created. They are executed when an object of the class is created just before the constructor is called. Non-static initializers are useful when you want to perform some common initialization logic for all objects of a class. They allow you to initialize instance variables in a concise and centralized manner, without having to repeat the same initialization code in each constructor.
While non-static initializers may have some limited use cases, they are rarely used and can be confusing for most developers because they only run when new class instances are created.
Non-static initializers should be refactored into standard constructors or field initializers when possible.
In most cases, the use of constructors, overloaded constructors, or factory methods is preferable for initializing instance variables. These approaches provide more explicit and controlled initialization, separate concerns, allow for better error handling, and make the code easier to understand and maintain.
class MyClass { private static final Map<String, String> MY_MAP = new HashMap<String, String>() { { put("a", "b"); } }; // Noncompliant - HashMap should be extended only to add behavior, not for initialization }
Using static initialization block:
class MyClass { private static final Map<String, String> MY_MAP = new HashMap<>(); static { MY_MAP.put("a", "b"); // Compliant } }
or using constructor:
class MyClass { private static final Map<String, String> MY_MAP = new HashMap<>(); public MyClass() { MY_MAP.put("a", "b"); // Compliant } }
or using Java 9 Map.of
:
class MyClass { private static final Map<String, String> MY_MAP = java.util.Map.of("a", "b"); // Compliant }
or using Guava ImmutableMap.of
:
class MyClass { private static final Map<String, String> MY_MAP = com.google.common.collect.ImmutableMap.of("a", "b"); // Compliant }