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

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

    Or simply use a if-elseif case :

    private final static int ONE = 1;
    private final static int TWO = 2;
    
    public static void main(String[] args) {
        int value = 1;
    
        if(value.equals(ONE)){
    
        }
        else if(value.equals(ONE)){
    
        }
    
    }
    

提交回复
热议问题