FindBugs and static initialization order

99封情书 提交于 2019-12-10 17:49:46

问题


I have the following Java code:

public class Something {

    static {
        new Something();
    } 

    public static final int[] EMPTY_INT_ARRAY = new int[0];
}

I'm using FindBugs to look for code errors, but the following error is never raised:

SI: Static initializer creates instance before all static final fields assigned (SI_INSTANCE_BEFORE_FINALS_ASSIGNED)

The class's static initializer creates an instance of the class before all of the static final fields are assigned.

Is this the correct case that should demonstrate this issue? why is it not picking up this code issue?


回答1:


Not tried, but I think an example would be:

public class Something {

    static {
        new Something();
    }

    public static final int CONST = 42;
}

As lysergic-acid has found out, this error has a lower severity in FindBugs (16). You have to manually lower the min threshold to make it show.



来源:https://stackoverflow.com/questions/20090807/findbugs-and-static-initialization-order

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!