Java switch : variable declaration and scope

后端 未结 7 686
予麋鹿
予麋鹿 2020-12-11 03:14

How does the Java compiler handle the following switch block ? What is the scope of the \'b\' variable ?

Note that the \'b\' variable is declared only in the first b

7条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-11 03:59

    The Scope of variables defined in a switch() statement would be the same as in a normal block wich is surround by { and }.

    Therefore every variable defined in a switch() statement is visible for the whole block, once it is defined.

提交回复
热议问题