Reports instance initializers which can be made static without producing any errors.

如果实例初始值设定项不引用其类的任何非 static 成员,则可以为 static。 Static initializers are executed once when the class is resolved, while instance initializers are executed on each instantiation of the class.

此检查不报告匿名类中的实例空初始值设定项和初始值设定项。

示例:


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

在应用快速修复后:


  class A {
     public static String CONSTANT;
     static {
        CONSTANT = "Hello"; // 现在每个类仅实例化一次
     }
  }