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)

  • Immediately written variable i is already defined in the method of main - Ziens
  • Yeah, and therefore case 10 It is worked out with a bang (if I have i on top of the case), read the code carefully, or run it. - Denis Kotlyarov
  • Why do you define i and max in case 10? - Ziens
  • Well, these are different blocks, what is declared in the internal block is not declared an automatic machine in the external. - etki
  • I just carefully read your code, and you, in turn, carefully read the comments. The announcement in case 10 is "visible" in case 11. - Ziens

3 answers 3

The point is visibility of variables. It is possible to declare one variable only once in one block of code.

In this case, in your case, at least the variable is declared in case 10 , if it is not assigned to it in case 11 will be considered declared, but not initialized. And, because this is not a class field, then no default values ​​will be assigned to it.

To make it work for you, you need to take these variables outside the switch block or in each case create a new variable with a new name.

  • Here, the fact is that he is a worker. "case create a new variable with a new name" aha and Java will still keep i, max in memory (and also these variables) (albeit empty ones). And it’s clear that Java creates, though empty, a lesson from this is clear that in the end you have to switch to if :) - Denis Kotlyarov
  • @DenisKotlyarov, case and if are different and one cannot say that one of them is better than the other. These are just different tools. At the initial stage, it’s not worth thinking about whether an extra variable is stored in memory. First, it is better to understand the basic things - in the operators, areas of visibility, etc. - YuriySPb
  • This feature can be very easy to explain, because we all know that the case has a break, (using the example of 10 11) if you remove the break from the 10 container, we get something that also 11 containers will be executed. Based on this, Java simply cannot deal with these variables, and therefore it creates all the variables (occurring more than once) at once. "At first it is better to understand the basic things - in the operators, areas of visibility, etc." I did not understand ... "At the initial stage," how did you decide. - Denis Kotlyarov
  • @DenisKotlyarov, I made my conclusion on the basis of the fact that you did not understand the switch operator and find it difficult to clearly formulate the question. In any case, I personally still can not understand what exactly you asked. - JuriySPb
  • I asked what kind of feature it was that variables in container 11 (similar to variables from container 10) can change their value without creating the variable itself. But after thinking and reading your comment, I understood this feature (which I voiced here above). - Denis Kotlyarov

They write the same:

Uncompilable source code - variable i is already defined

Just accept that you can't do that. Or take out the necessary variables above switch:

 int i = 0; int max = 2; switch( type ) { // ... 

Or limit their visibility inside each case:

 case 10: { int i = 0; int max = 2; // ... } break; case 11: { int i = 0; int max = 2; // ... } break; 

Here, the thing is, he's a worker.

Only not so good worker.

    The fact is that the case labels inside a switch are not some kind of "containers" as you perceive them.

    The switch / case block itself was copied into Java from C-like languages. And in them the syntax of labels of the form case <value>: followed the general syntax of labels label: Which, in fact, were just named places in the code in which you could jump at any moment. Using goto :

     int main() { for (...) { for (...) { if (something) goto stop; } } // some code stop: // some code } 

    switch , in fact, simply allowed you to select a target label for the jump based on the value.

    Java copied the switch , but did not copy the goto . Therefore, the behavior seems strange to you.

    If your code is formatted so that the labels no longer seem to be part of the code, and they began to be read exactly as marks for a jump, the mechanism of the switch operation will become more obvious:

     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; // <======= break; } } 

    For the same reason, at the end of the block for each label you have to write break; so that the implementation does not "fail" further.