When are references declared in a switch statement?

Posted by sandis on Stack Overflow See other posts from Stack Overflow or by sandis
Published on 2010-06-17T07:56:35Z Indexed on 2010/06/17 8:03 UTC
Read the original article Hit count: 341

Filed under:
|

To my surprise this code works fine:

int i = 2;
switch(i) {
case 1:
    String myString = "foo";
    break;
case 2:
    myString = "poo";
    System.out.println(myString);
}

But the String reference should never be declared? Could it be that all variables under every case always are declared no matter what, or how is this resolved?

© Stack Overflow or respective owner

Related posts about java

Related posts about switch-statement