Inspectopedia Help

Class initializer may be 'static'

Reports instance initializers which may be made static.

An instance initializer may be static if it does not reference any of its class' non-static members. Static initializers are executed once the class is resolved, while instance initializers are executed on each instantiation of the class.

This inspection doesn't report instance empty initializers and initializers in anonymous classes.


class A { public static String CONSTANT; { CONSTANT = "Hello"; } }

After the quick-fix is applied:

class A { public static String CONSTANT; static { CONSTANT = "Hello"; //now initialized only once per class } }

Inspection Details

Available in:

IntelliJ IDEA 2023.3, Qodana for JVM 2023.3


Java, 233.SNAPSHOT

Last modified: 13 July 2023