Java label irregularity (possible bug?) -


if @ java standard §14.7, see statements may have label prefixes, e.g.:

labeledstatement:

     identifier : statement

in theory, label should able label succeeding statement. so, example, following compiles accordingly:

public class test {     public static void main(string[] args) {     hello:         return;     }  } 

intuitively, compiles:

public class test {     int i;     public static void main(string[] args) {         test t = new test();     label:         t.i = 2;             } } 

but following not compile:

public class test {     public static void main(string[] args) {     oops:         int k = 3;       } } 

even though (note scoped brackets):

public class test {     public static void main(string[] args) {     oops:         {             int k = 3;         }     } } 

so question hinges on whether or not declarations statements. according standard (and online documentation):

in addition expression statements, there 2 other kinds of statements: declaration statements , control flow statements. declaration statement declares variable.

i've noticed behavior in java 7 , 8 on both osx windows. bug or misunderstanding standard?

the expression

int k = 3;  

is local variable declaration statement.

the statement used in syntax of label statement

labeledstatement:

  identifier : statement

does not contain local variable declaration statements. therefore can't use them within labeled statement directly.

local variable declaration statements can used within blocks can used within labeled statements.


Comments