Home>
Question

The break statement in LABEL1: {} in the corresponding source code,
It does not target LABEL1, but switches (i) {}.
I would like to know the reason why LABEL 1 is not applicable.
I was able to set the target to LABEL1 by specifying the label in the break statement,
If i know the reason why it is not applicable when you do not specify it, please teach us.
Thank you.

Corresponding source code
class switchcase
{
    public static void main (String [] args)
    {
        int i = 1;
        switch (i) {
        case 0:
                System.out.println ("0");
                break;
        case 1:
                // I want to know why the LABEL1 block is not subject to break
                LABEL1: {
                    System.out.println ("5");
                    break;
                }
                System.out.println ("No control is transferred to this statement");
        default: default:
            {
                System.out.println ("default");
                break;
            }
        }
        System.out.println ("Exit");
    }
}
Supplementary information (FW/tool version, etc.)

openjdk is version "1.8.0_41"

Compiling and running on Windows command prompt

  • Answer # 1

    That's what the Java specifications say.

    Abreakstatement with no label attempts to transfer control to the innermost enclosingswitch,while,do, orforstatement of the immediately enclosing method or initializer;this statement, which is called the break target, then immediately completes normally.