Inspectopedia 2025.2 Help

Unsafe lazy initialization of 'static' field

Reports static variables that are lazily initialized in a non-thread-safe manner.

Lazy initialization of static variables should be done with an appropriate synchronization construct to prevent different threads from performing conflicting initialization.

When applicable, a quick-fix, which introduces the lazy initialization holder class idiom, is suggested. This idiom makes use of the fact that the JVM guarantees that a class will not be initialized until it is used.

Example:

class X { private static List<String> list; public List<String> getList() { if (list == null) { list = List.of("one", "two", "tree"); } return list; } }

After the quick-fix is applied:

class X { private static final class ListHolder { static final List<String> list = List.of("one", "two", "tree"); } public List<String> getList() { return ListHolder.list; } }

Locating this inspection

By ID

Can be used to locate inspection in e.g. Qodana configuration files, where you can quickly enable or disable it, or adjust its settings.

NonThreadSafeLazyInitialization
Via Settings dialog

Path to the inspection settings via IntelliJ Platform IDE Settings dialog, when you need to adjust inspection settings directly from your IDE.

Settings or Preferences | Editor | Inspections | Java | Initialization

Suppressing Inspection

You can suppress this inspection by placing the following comment marker before the code fragment where you no longer want messages from this inspection to appear:

//noinspection NonThreadSafeLazyInitialization

More detailed instructions as well as other ways and options that you have can be found in the product documentation:

Inspection Details

By default bundled with:

IntelliJ IDEA 2025.2, Qodana for JVM 2025.2,

Last modified: 18 September 2025