Explicit label unevenness (maybe a mistake?)

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?

+8
java standards java-8 label
source share
1 answer

Expression

 int k = 3; 

is a directive to declare a local variable .

statement used in label operator syntax

LabeledStatement :

Identifier : statement

does not contain local variable declaration statements. Therefore, you cannot use them in an expression labeled directly.

Declaration operators of local variables can be used in blocks that can be used in labeled statements.

+8
source share

All Articles