Why are variables not local in case statements?

前端 未结 6 1454
暖寄归人
暖寄归人 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:32

    Matthew is right - the whole switch statement has one scope for any variables declared directly within it. You can add more braces as per Matthew's answer - but it would almost certainly be better to extra the case bodies out as methods. It looks like they're doing rather a lot to be included "inline" like that.

    Note that the scoping rules here aren't specific to Android - they're Java's scoping rules.

提交回复
热议问题