Why are variables not local in case statements?

前端 未结 6 1455
暖寄归人
暖寄归人 2020-12-10 14:03

I recently add another menu item to an android java app and was suprised that Eclipse said that variable from the previous case:break were not local (So I\'ve just added a s

6条回答
  •  伪装坚强ぢ
    2020-12-10 14:24

    You're right that at most one will execute, but a case does not create a new scope. You can manually create a block with its own scope.

    case foo:
        {
           int var = ...
        }
        break;
    
    case bar:
        {
           int var = ...
        }
        break;
    

提交回复
热议问题