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?
Well, it’s about brackets (i.e. scope).
It’s better practice, arguably, to write your statements like so:
(I’m not near a Java compiler right now, but that shouldn’t compile).