3

Why I cannot create variables in a each case with same name. For example, from the code below, in case 3 it will complain that String name already exists - but why would it? Case 1 has never been and will never be called.

I would not want to extract the definitions out of the switch-case. So why using if statements it lets to define same names in each case, but in switch-case it doesn't?

Here is a simple Java code with a switch case:

int type = 3;

switch (type) {
    case 1:
        String name = (String) respone.get("name");
        user.setName(name);
        break;
    case 2:
        String surname = (String) respone.get("surname");
        user.setSurname(surname);
        break;
    case 3:
        String name = (String) respone.get("name");
        user.clearName(name);
        break;
    default:
        ...
}
Arturs Vancans
  • 4,531
  • 14
  • 47
  • 76
  • 1
    The answer might as well be "because". I'm guessing that it's because the `case:` blocks aren't really blocks as far as the compiler is concerned - they're goto labels in a single `switch` block. So the scope of local variables is that switch block. – millimoose Mar 20 '13 at 16:06
  • You could always put directly the casted `(String) respone.get("foo")` in the function you call ... – LaGrandMere Mar 20 '13 at 16:07
  • A good question, but already answered many times, eg duplicate of: http://stackoverflow.com/a/3894215/1081849 – sbk Mar 20 '13 at 16:08

6 Answers6

7

Write each case statement in a different block:

switch (type) {
    case 1: {
        String name = (String) respone.get("name");
        user.setName(name);
        break;
    }
    case 2: {
        String surname = (String) respone.get("surname");
        user.setSurname(surname);
        break;
    }
    ...
}
Javier
  • 12,100
  • 5
  • 46
  • 57
4

name already exists because the cases are conceptually in the same block. The case: is essentially a label and the switch statement simply jumps to the appropriate label. See here for more info on labels in Java (not restricted to switch statements).

To see this, imagine if you didn't have the break statements. You could initialise name in one case clause, and fall through to the next clause. Enclose each case clause in a separate {} block if this is a problem.

Brian Agnew
  • 268,207
  • 37
  • 334
  • 440
0

Because even if they are mutually exclusive, they are present within the same Switch block. Hence, you can write each case clause within separate blocks, to avoid that error.

Rahul
  • 44,383
  • 11
  • 84
  • 103
0

Keep in mind that case is not your scope, the blocks are { .. }

OnResolve
  • 4,016
  • 3
  • 28
  • 50
0

The whole switch is contained in one block. You could wrap each case in an own block:

case 1: {
    String name = (String) respone.get("name");
    user.setName(name);
    break;
  }
case 2: {
    String surname = (String) respone.get("surname");
    user.setSurname(surname);
    break;
  }
case 3: {
    String name = (String) respone.get("name");
    user.clearName(name);
    break;
  }
Kai
  • 38,985
  • 14
  • 88
  • 103
0

If you declare the variable without braces in switch then its scope remain throughout the switch.

Its no matter where you declare its first switch block or second. If you declare it first then its can be access after it through out switch.

If you want to have same variable name then you can do it by putting in braces or block because then that variable scope remain within the block.

switch (type) {
    case 1: {
        String name = (String) respone.get("name");
        user.setName(name);
        break;
    }
    case 2: {
        String surname = (String) respone.get("surname");
        user.setSurname(surname);
        break;
    }
    case 3: {
        String name = (String) respone.get("name");
        user.clearName(name);
        break;
    }
    default:
        ...
}
Ajay S
  • 48,003
  • 27
  • 91
  • 111