Variable's scope in a switch case

JavaSwitch Statement

Java Problem Overview


I think I don't understand how the scope works in a switch case.

Can someone explain to me why the first code doesn't compile but the second does ?

Code 1 :

 int key = 2;
 switch (key) {
 case 1:
      String str = "1";
      return str;
 case 2:
      String str = "2"; // duplicate declaration of "str" according to Eclipse.
      return str;
 }

Code 2 :

 int key = 2;
 if (key == 1) {
      String str = "1";
      return str;
 } else if (key == 2) {
      String str = "2";
      return str;
 }

How come the scope of the variable "str" is not contained within Case 1 ?

If I skip the declaration of case 1 the variable "str" is never declared...

Java Solutions


Solution 1 - Java

I'll repeat what others have said: the scope of the variables in each case clause corresponds to the whole switch statement. You can, however, create further nested scopes with braces as follows:

int key = 2;
switch (key) {
case 1: {
    String str = "1";
    return str;
  }
case 2: {
    String str = "2";
    return str;
  }
}

The resulting code will now compile successfully since the variable named str in each case clause is in its own scope.

Solution 2 - Java

The scope of the variable is the whole switch statement -- all cases and default, if included.

Here are some other options...

Option 1:

int key = 2;
switch (key) {
case 1:
     return "1";
case 2:
     return "2";
}

Option 2:

int key = 2;
String str = null;
switch (key) {
case 1:
     str = "1";
     return str;
case 2:
     str = "2";
     return str;
}

Solution 3 - Java

You seem to be assuming that each case is a block with its own local scope, as if/else blocks. It's not.

It's important to correct this conceptual mistake, because otherwise you'll end falling in the frequent trap of forgetting the break inside the case

Solution 4 - Java

I think it's a valid question, and scope assumption for case statment is unavoidable. Adjusting ourselves to it because java writer has made this not correct.

e.g. if statement by default take first line in its scope than what's wrong with cases where the end of case is explicitly closed by break statement. Hence the declaration in case 1: should not be available in case 2 and it has parallel scope but not nested.

Solution 5 - Java

Several cases can be executed in one switch statement. So..

Solution 6 - Java

The scope of a variable exists between the braces of the switch and if statements. In example Code 1 the switch braces enclose both declarations of the variables which will cause the compiler to error as the name to variable binding will have already been made.

In the other example it is ok because both variables are declared within their own braces (scope).

Solution 7 - Java

In the first case the scope of the String declaration is within the switch statement therefore it is shown as duplicate while in the second the string is enclosed within curly braces which limits the scope within the if/else conditions,therefore it is not an error in the second case.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionPhilippe CarriereView Question on Stackoverflow
Solution 1 - JavaRichard CookView Answer on Stackoverflow
Solution 2 - JavaDrew WillsView Answer on Stackoverflow
Solution 3 - JavaleonbloyView Answer on Stackoverflow
Solution 4 - JavaSachin KumarView Answer on Stackoverflow
Solution 5 - JavaStan KurilinView Answer on Stackoverflow
Solution 6 - JavaJB.View Answer on Stackoverflow
Solution 7 - JavaEmilView Answer on Stackoverflow