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?

有帮助吗?

解决方案

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.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top