views:

143

answers:

8

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: {
    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.

+4  A: 

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

Thorbjørn Ravn Andersen
Then why does C work that way? ;]
Zenzen
Because the Unix-folks at Bell Labs needed a portable assembler, and the switch statement without blocks mapped very well to their target machine code output.
Thorbjørn Ravn Andersen
+5  A: 

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.

erickson
+6  A: 

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.

Randolpho
Highly agree, and even if you *did* want to do too much in a case block, you still wouldn't need separate scopes. Remember the whole reason scope was invented was so `account` in one block is different than `account` in the other, which is not a concern when the scopes never overlap.
Karl Bielefeldt
+1  A: 

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.

DVK
+2  A: 

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.

Dana Leonard
+2  A: 

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

Andreas_D
Actually I prefer linkers to be _light_.
Thorbjørn Ravn Andersen
A: 

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.

Tejo
How, exactly, is that relevant to scoping?
Thorbjørn Ravn Andersen
Well, he could use the "account" in any of the "cases".
Tejo
A: 

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; }
}
emory