Who can explain the behavior of the case
and is it considered the norm? :) Here is the working code
package javaapplication12; public class JavaApplication12 { public static void main(String[] args) { int type = 11; switch(type){ case 10: int i = 0; int max = 3; while(i<max){ System.out.println("OK"); i = i + 1; } break; case 11: i = 0; // <======= max = 2; // <======== //МЫ ИСПОЛЬЗОВАЛИ КАК ОБЫЧНО INT NAME = 0 //но компилятор выбрасывает //Exception in thread "main" java.lang.RuntimeException: Uncompilable source code - variable i is already defined in method main(java.lang.String[]) //????? уберем INT, => NAME = 0 компилируется и работает, КАК ЭТО? переменная int i из другого case создается? но как while(i<max){ System.out.println("WHAT??"); i = i + 1; } break; } } }
Java 8. Netbeans. And if you comment i = 0; or max = 2; get an exception :) (the code wrapped in main for clarity)