Can we call a “case” inside another case in the same switch statement in Java?

后端 未结 5 1554
傲寒
傲寒 2021-01-04 10:28

My intention is to call two cases inside another case in the same switch statement,

switch (orderType) {
        case 1: 
            statement 1;
                   


        
5条回答
  •  傲寒
    傲寒 (楼主)
    2021-01-04 11:08

    Although you cannot influence the switch cases directly, you can call switch's parent method from one case and pass different arguments. For example,

    void foo(int param1, String param2, ...) {
        switch (param1) {
            case 0:
                foo(1, "some string");
                break;
    
            case 1:
                //do something
                break;
    
            default:
                break;
        }
    }
    

提交回复
热议问题