If we look at the Java standard §14.7, we will see that operators can have shortcut prefixes, for example:
LabeledStatement:
ID: Statement
Theoretically, the label should be able to label any subsequent statement. So, for example, the following compilation, respectively:
public class Test { public static void main(String[] args) { hello: return; } }
Intuitively, this also compiles:
public class Test { int i; public static void main(String[] args) { Test t = new Test(); label: ti = 2; } }
But the following: does not compile:
public class Test { public static void main(String[] args) { oops: int k = 3; } }
Despite this (note the brackets):
public class Test { public static void main(String[] args) { oops: { int k = 3; } } }
So the question depends on whether the statements are statements. According to standard (and online documentation ):
In addition to expression statements, there are two other kinds of statements: declaration statements and control flow statements. The declaration statement declares a variable.
I noticed this behavior in Java 7 and 8 on both OSX and Windows. Is this a mistake or do I not understand the standard?
java standards java-8 label
David titarenco
source share