Why, in Java, is a variable's scope confined to a switch block as opposed to a case block. For example,
// Scope limited to a switch block
switch (number) {
case 1:
String result = "...";
break;
case 2:
result = "...";
break;
In the above example, result needs only to be declared once. If you declare it twice then you receive a Duplicate local variable
message.
My question is: how does the program know you've declared result
if number = 2
?
(It won't fall into case 1
and won't declare the variable... or will it?)
EDIT:
I might be confusing everyone. I understand how I can limit the scope of a variable but my question is: how does Java know that result has been declared if it doesn't fall into the case?