Volume b is the switch block - between declaration and 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 int b inside case 1: you will NOT have access to the variable b inside case 0:
To answer the question you ask in java comments, you can check out this simpler example:
int b; if(true){ b++;
Hope this helps.
Cacho santa
source share