开发者

Why are different case condition bodies not in different scope?

开发者 https://www.devze.com 2023-01-13 03:27 出处:网络
Why are different case bodies not automatically in their own scope?For example, if I were to do this:

Why are different case bodies not automatically in their own scope? For example, if I were to do this:

switch(condition) {
  case CONDITION_ONE:
    int account = 27373;
  case CONDITION_TWO:
    // account var not needed here
  case CONDITION_THREE:
    // account var not needed here
  case CONDITION_FOUR:
    int account = 90384;
}

the compiler would complain about local variable redefinitions. I understand I could do this:

switch(condition) {
  case CONDITION_ONE: {
    int account = 27373;
  }
  case CONDITION_TWO: {
    // account var not needed here
  }
  case CONDITION_THREE: {
    // account var not needed here
  }
  case CONDITION_FOUR: {
    开发者_JAVA技巧int account = 90384;
  }
}

to put a block around each set of statements to be executed to put each account variable in its own scope. But why doesn't the language do this for me?

Why would you ever want to declare a local variable in CONDITION_ONE's body and then use it in CONDITION_TWO's? This seems like a TERRIBLE idea which should be explicitly banned, not implicitly permitted.


Why would you want this? If you need a new scope for each case block, you're doing too much in your case block. Push that off to a method.


That would be inconsistent with the rest of the language.

As it is, scope is always determined by blocks. That sort of consistency makes Java easier to read and maintain.


Because that is how C works and Java was designed to lure C programmers over.


To add to the other answers, you would also lose the benefits of fallthrough should a subsequent case need to be in the same scope as the previous one. As far as I know, its much easier to add a new level of scope than try to escape one forced upon you by the language.


I'm happy that it exactly like that. The scope of a local variable is always a block. One single rule, no exceptions.

One Block to rule them all, One Block to find them,
One Block to bring them all and in the darkness bind them


Lucky enough for you, you're in a good company in not liking this behavior - Jon Skeet agrees with you :)

Yes, I know the link is to a question about C, but this Java behavior is inherited from C's block scoping rules.


Because, you may want to do this:

switch(condition) {
  case CONDITION_ONE:
    int account = 27373;
  case CONDITION_TWO:
    DoStuffHere();
  case CONDITION_THREE:
  case CONDITION_FOUR:
    DoMoreStuffHere();
}

then, and with this... if you got "CONDITION_ONE", then the variable would be set, and both DoStuffHere and DoMoreStuffHere would be called. But, if otherwise you got CONDITION_THREE, then, only DoMoreStuffHere would be called.


The answer is as others have said - more or less - because that is the way things work.

Perhaps Tom Tresansky really meant how to achieve the desired effect.

switch(condition) {
  case CONDITION_ONE:
    { int account = 27373; }
  case CONDITION_TWO:
    // account var not needed here
  case CONDITION_THREE:
    // account var not needed here
  case CONDITION_FOUR:
    { int account = 90384; }
}
0

精彩评论

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

关注公众号