Inspectopedia
 
2024.3

Unsafe lazy initialization of 'static' field

Warning
New
Last modified: 03 December 2024

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:

After the quick-fix is applied: