You get an error because a label cannot be applied to variable declarations, that’s just how the language grammar is defined (a label can only precede a Statement
, and a LocalVariableDeclarationStatement
is not a Statement
). The reason is probably that it could cause confusion concerning variable scope. This works:
label1: System.out.println("");
label2: { LabelTest t = new LabelTest(); }