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

后端 未结 5 1558
傲寒
傲寒 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 10:44

    No, you can't jump to the code snippet in another switch case. You can however extract the code into an own method that can be called from another case:

    switch (orderType) {
        case 1: 
            someMethod1();
            break;
        case 2:
            someMethod2();
            break;
        case 3:
            someMethod1();
            someMethod2();
            break;
        default:
            break;
    }
    
    void someMethod1() { ... }
    void someMethod2() { ... }
    

提交回复
热议问题