The break
statement is talked about by the JLS, Section 14.15:
A break statement with no label attempts to transfer control to the
innermost enclosing switch, while, do, or for statement of the
immediately enclosing method or initializer; this statement, which is
called the break target, then immediately completes normally.
To be precise, a break statement with no label always completes
abruptly, the reason being a break with no label.
If no switch, while, do, or for statement in the immediately enclosing
method, constructor, or initializer contains the break statement, a
compile-time error occurs.
A break statement with label Identifier attempts to transfer control
to the enclosing labeled statement (ยง14.7) that has the same
Identifier as its label; this statement, which is called the break
target, then immediately completes normally. In this case, the break
target need not be a switch, while, do, or for statement.
To be precise, a break statement with label Identifier always
completes abruptly, the reason being a break with label Identifier.
A break statement must refer to a label within the immediately
enclosing method, constructor, or initializer. There are no non-local
jumps. If no labeled statement with Identifier as its label in the
immediately enclosing method, constructor, or initializer contains the
break statement, a compile-time error occurs.
There is nothing that states that the label to which break
breaks can't be labelled to something inside the current block. You can always just say break;
to break out of the innermost contained block.
And break
doesn't break out of an if
-- it must be a switch
, while
, do
, or for
, per the JLS above.
The reason that label: Object o = new Object();
doesn't work is because Object o = new Object();
isn't a statement -- it's a declaration. The JLS, Section 14.7 talks about that:
The Identifier is declared to be the label of the immediately contained Statement.