开发者

Why are variables not local in case statements?

开发者 https://www.devze.com 2023-01-23 09:39 出处:网络
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 suffix to get by).

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 suffix to get by).

I'm a bit confused as in my mind, the 1st set of case:break would not be executed at all if the 2nd option was chosen. Could someone explain my faulty thinking please?

        case R.id.menuDebugMode:
            debugMode = !debugMode;
            if (debugMode){
                Toast.makeText(mainActivity.this, "Debug Mode on - NOT TO BE USED WHILST DRIVING", Toast.LENGTH_LONG).show();           
            } else {
                tvDebug.setText("");
                tvInfo.setText("");
            }
            SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
            SharedPreferences.Editor editor = settings.edit();
            editor.putBoolean("debugMode", debugMode);
            editor.commit();
        break;

        case R.id.menuSpeedMode:
            speedSignMode = !speedSignMode;
            if (speedSignMode){
                Toast.makeText(mainActivity.this, "SpeedSign Mode in use", Toast.LENGTH_LONG).show();           

            } else {
                    Toast.makeText(mainActivity.this, "MapSpeed Mode in use", Toast.LENGTH_LONG).show();            
            }
            SharedPreferences settings2 = getSharedPreferences(PREFS_NAME, 0);
            SharedPreferences.Editor editor2 = s开发者_StackOverflow中文版ettings2.edit();
            editor2.putBoolean("speedSignMode", speedSignMode);
            editor2.commit();
        break;`


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;


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:


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


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.


Because you're not supposed to write much code / logic there - break these out into methods.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消