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
The scope of b
is the block. You have only one block which includes all case
s. That's why you get a compile error when you redeclare b
in your second case
.
You could wrap each case
in an own block like
case 0:
{
int b = 1;
...
}
case 1:
{
int b = 2;
...
}
but I think FindBugs or CheckStyle would complain about that.