3

I found this case a bit peculiar:

    int x = 1;
    switch(x){
          case 0 :
             boolean b = false;
             break;
          case 1 :
             b = true; //will compile just right
             System.out.println(b); //will print true
             break;
          default:
                 System.out.println(b); //will not compile
       }

I just think this is confusing. Local variables are said to be accessible within the entire block of codes. Sure, b is within a switch block so I can understand it is accessible in a different case, even though this seems to contradict the switch flow that it may not go through the first case, and therefore, may not declare and initialize b.

But isn't this again contradicting if b meanwhile is still inaccessible in the default branch?

Is there any special execution for local variable declarations in a case branch that it will run whether the case has been matched or not? If yes, then why can't default branch access it?

Edit for the duplicate flag: The question here is not about how the variable declared in a case branch is not local (as I noted above), but instead, why the local variable declared inside a case is accessible outside it, but not the value it has been initialized with.

Found a similar question that I believe answers my question: Declaring and initializing variables within Java switches

christianleroy
  • 1,084
  • 5
  • 25
  • 39

4 Answers4

2

The key point is that the default case means: no other case was taken.

The switch construct allows you to have local variables within its overall scope, but still each "read" must see a prior "write".

And the compiler can easily detect that the default case didn't see a initialization for b.

Beyond that, the reason why you can have such assignments in different cases is: the overall switch statement only has one "switch block".

See the Java Language Spec for further details!

GhostCat
  • 137,827
  • 25
  • 176
  • 248
  • And the "make the code correct" fix is to move the declaration and initialization of ```b``` to before the switch. –  Mar 03 '19 at 13:25
1

b should be accessible in the default block, but you did not initialize it.

Try:

default: 
    b = true; 
    System.out.println(b); 
xingbin
  • 27,410
  • 9
  • 53
  • 103
  • I see and understand. But how come the declaration of 'b' has been "recognized" inside a case that may or may not be run, but not its initialization? – christianleroy Mar 03 '19 at 10:23
  • short story: because Java is specified that way (roughly: variable is in scope inside the block where the it is declared; initialization is a statement that is executed in the order in appears) – user85421 Mar 03 '19 at 10:33
1

b scope is the switch block. The reason of the compilation error is b in the default block might not be initialized.

If you want b to exists only in the case block then you need to wrap it with {}:

switch(x) {
  case 0 : {
    boolean b = false;
    break;
  }
Adam Siemion
  • 15,569
  • 7
  • 58
  • 92
0

if x=1 it will execute case 1: and since you have a breakstatement in case 1:, once it encounters the breakstatement it exits out of the switch and will never reach the default case. So if you want the program to execute the default case either change xto another number other than 0 and 1 or remove the breakstatement from case 1:

Wanmi Siangshai
  • 133
  • 1
  • 3
  • 8