Code: Select all
public class Switcher{
public static void main(String[] args){
switch(Integer.parseInt(args[1])) //1
{
case 0 :
boolean b = false;
break;
case 1 :
b = true; //2
break;
}
if(b) System.out.println(args[2]);
}
}
While it is declared in case 0:, these lines won't execute if args[1] is 1, so how does the application know that b should be a boolean where it matches case 1:?
I tried commenting out the problematic print line and declaring boolean b = true; at //2, but the compiler complained that b is already defined.
Can I assume that any declarations in the first case are visible to all the other cases?
Also, what happens if I declare a variable in one of the later cases? Is this visible from anywhere in the switch, or only in the cases that follow it?