Why we can not define a variable twice in switch?
java, switch-statement
Solution
if the break statements are not used, Then we know that the following cases will be executed. So if you are permitted to declare in both cases that will cause a conflict. For example
switch(x) {
case 1:
int a = 1; // i have removed the break;
case 2:
int a = 2; // now what will happen here :)
break;
}
Problem
Why this is illegal: ``` switch(x) { case 1: int a = 1; break; case 2: int a = 2; break; } ``` It looks like it could have been legal. What are some deeper reasons for that to be illegal?