10

How does the Java compiler handle the following switch block ? What is the scope of the 'b' variable ?

Note that the 'b' variable is declared only in the first branch of the switch statement. Attempting to declare it in the second branch as well results in a "duplicate local variable" compilation error.

    int a = 3;
    switch( a ) {
    case 0:
        int b = 1;
        System.out.println("case 0: b = " + b);
        break;
    case 1:
        // the following line does not compile: b may not have been initialized
        // System.out.println("case 1 before: b = " + b);
        b = 2;
        System.out.println("case 1 after: b = " + b);
        break;
    default:
        b = 7;
        System.out.println("default: b = " + b);
    }

Note: the above code compiles with a java 1.6 compiler.

Marius Ion
  • 387
  • 3
  • 10

7 Answers7

22

The scope is, just as usual, delimited by { and }.

aioobe
  • 413,195
  • 112
  • 811
  • 826
  • 7
    Message to OP, you can put braces around each case and then it would work. As in `case 1: { doHere(); break; }` – jn1kk Jun 07 '12 at 13:43
12

The scope of b is the block. You have only one block which includes all cases. That's why you get a compile error when you redeclare b in your second case.

You could wrap each case in an own block like

case 0:
   {
     int b = 1;
     ...
   }
case 1:
   {
     int b = 2;
     ...
   }

but I think FindBugs or CheckStyle would complain about that.

Kai
  • 38,985
  • 14
  • 88
  • 103
  • 5
    The compile error is not from _redeclaring_ `b` but from accessing it before it is initialized. `b` is perfectly valid in case 0, 1, and default because as you mention it is in scope. The issue with his commented line not compiling is that b is not initialized before it is accessed. – NominSim Jun 07 '12 at 14:07
  • I should have read the question more careful. I thought he was doing `int b = 2` in the commented out line ;-) – Kai Jun 07 '12 at 15:10
4

The scope of b is the switch block - between the declaration and the delimiter } -

int a = 3;

switch( a ) {
     case 0:
          int b = 1; //scope starts
          System.out.println("case 0: b = " + b);
          break;
     case 1:
          // the following line does not compile: b may not have been initialized
          // System.out.println("case 1 before: b = " + b);
          b = 2;
          System.out.println("case 1 after: b = " + b);
          break;
     default:
          b = 7;
          System.out.println("default: b = " + b);
}//scope ends

However, you need to know that if you declare the int b inside the case 1:, you will NOT have access to the variable b inside the case 0:

To answer the question you ask in the java comments you can check this simpler example:

int b;
if(true){
    b++; //The local variable b hast not been initialized
}

Hope it helps.

Cacho Santa
  • 6,846
  • 6
  • 41
  • 73
4

You can define the scope using {} around your case.

int a = 3;
switch( a ) {
case 0: {
    int b = 1;
    System.out.println("case 0: b = " + b);
    break;
}
case 1: {
    // the following line does not compile: b may not have been initialized
    // System.out.println("case 1 before: b = " + b);
    int b = 2;
    System.out.println("case 1 after: b = " + b);
    break;
}
default: {
    int b = 7;
    System.out.println("default: b = " + b);
}
}
Sungray
  • 41
  • 2
1

in your code if a is not equal to 0 b will never be initialized. you should define b before the switch statement.

hdayi
  • 423
  • 2
  • 4
  • 13
0

Your case blocks do not have any local scope. It's not a series of if...else if...else blocks, java implements it as a series of GOTOs.

Hans Z
  • 4,664
  • 2
  • 27
  • 50
0

The Scope of variables defined in a switch() statement would be the same as in a normal block wich is surround by { and }.

Therefore every variable defined in a switch() statement is visible for the whole block, once it is defined.

Thkru
  • 4,218
  • 2
  • 18
  • 37