Why “final static int” can be used as a switch's case constant but not “final static

后端 未结 5 2091
遥遥无期
遥遥无期 2021-02-12 13:21

Why is this int switch valid:


public class Foo {
    private final static int ONE = 1;
    private final static int TWO = 2;

    public static void main(String         


        
5条回答
  •  日久生厌
    2021-02-12 13:56

    Because a case statement label must have either a compile time constant or an EnumConstantName. JLS 14.11

    Compile time constants can only be strings and primitive types, as described by JLS 15.28. Thus you can not use a static final , as it is neither a compile time constant, nor the name of an enum.

提交回复
热议问题