I recently add another menu item to an android java app and was suprised that Eclipse said that variable from the previous case:break were not local (So I\'ve just added a s
In additon to the other answers: here's the explanation from the java language definition:
Whenever the flow of control enters a block [...], a new variable is created for each local variable declared in a local variable declaration statement immediately contained within that block [...] The local variable effectively ceases to exist when the execution of the block [...] is complete.
A local variable scope is the block ({ ... }
), which includes inner blocks. The actual block in your code is the block starting after the switch
statement.
Others have explained what you should do, and that this is a Java language thing, not an Android specific thing.
As to why the Java Language is defined this way, I haven't figured an entirely logical reason. The best I can think of is that if each of the case lists of a switch statement implicitly defined a scope, then the following could easily be misread:
case foo:
int var = ...
// note drop through
case bar:
int var = ...
var = var + 1;
break;
At least with the current definition of the scoping, all of the potentially confusing usages result in compilation errors.
(IMO, it would have been better to eschew case drop-through in switch statements ... just like C# does. But design mistakes like that are much easier to spot in hindsight, and are hard to correct once made.)
Because you're not supposed to write much code / logic there - break these out into methods.
As in C, in Java a switch statement is not what one would expect when looking at it. The indendation makes it difficult to understand that a scope is not created. This all boils down to C, where a switch is just syntactic sugar. The compiler transforms a switch into a number of conditional jumps. This enables the language to use fall-through, a feature that during the design of C was intended ("break" remained optional). This Java feature remained compatible to C.
switch(a):
case 1:
dosomething();
case 2:
dosomemore();
gets translated into
if(a==1) jump ##1;
if(a==2) jump ##2;
jump ##3;
##1:
dosometing();
##2:
dosomemore();
##3:
You're right that at most one will execute, but a case does not create a new scope. You can manually create a block with its own scope.
case foo:
{
int var = ...
}
break;
case bar:
{
int var = ...
}
break;
Matthew is right - the whole switch statement has one scope for any variables declared directly within it. You can add more braces as per Matthew's answer - but it would almost certainly be better to extra the case bodies out as methods. It looks like they're doing rather a lot to be included "inline" like that.
Note that the scoping rules here aren't specific to Android - they're Java's scoping rules.