Thanks for all the quick and useful answers - I marked TME as the accepted one (as it had the clearestanswer to the why :) but all the answers were good ones. I am normally an if then elseif type programer so that's why I've been caught out here :)
–
SimpleSiNov 5 '10 at 9:29

6 Answers
6

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.

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:

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.)

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.

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.