Possible Duplicate:
Why are variables not local in case statements?
A variable defined in a scope block can’t be used outside it. For example, the following code snippet is invalid:
{
int anothervar = 4;
}
{
System.out.println(anothervar);
}
But it looks like a case block does not create apart scopes.
switch (mode) {
case ONE:
dosomething();
return;
case TWO:
int[] someints = new int[] { 2, 3, 5, 7 };
SomeObject obj = new SomeObject();
return;
case THREE:
someints = new int[] { 1, 4, 6, 8, 9 };
obj = new SomeObject();
return;
}
Why don’t I have to declare someints inside the case THREE ‘block’?
Suppose mode = THREE, then the declaration of variable someints is never reached, because case TWO, where someints is declared, is skipped. Or isn’t it? How does it work internally?
(The chosen answer in Why are variables not local in case statements? states that a switch statement is internally a set of jump commands, but still that does not explain where the variable someints is declared.)
a local variable’s scope is inside a block, as specified in the Names documentation:
A block is defined to have enclosing braces, as written as well in the Blocks and Statements documentation: