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

后端 未结 5 2089
遥遥无期
遥遥无期 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:38

    The compiler says

    unqualified enumeration constant name required
    

    So your value of RT would need to be RUNTIME instead of RetentionPolicy.RUNTIME to make your code work. But of course that is not possible. Why not use the RetentionPolicy enum directly? If you want to stick to your final static declaration, you need to assign the whole enum to your final static variable.

提交回复
热议问题