error:undefined label, how to use label statement in this code in java? -


i read in textbooks java statement can labeled , can used break. while trying code error undefined label. (guys @ stackoverflow wait before marking question duplicate, have checked questions none of explain problem).

public class labeltest {      public static void main(string[] args) {          first: system.out.println("first statement");         (int = 0; < 2; i++) {             system.out.println("second statement");             break first;         }     } } 

as per jls 14.7

the scope of label of labeled statement contained statement.

so in case, scope of lable first sysout statement following lable. clearer, can define scope using curly braces, , within these braces valid jump label. below valid

first: {         system.out.println("first statement");         (int = 0; < 2; i++) {             system.out.println("second statement");             break first;         }     } 

or

first: {     system.out.println("first statement");     break first; } second: for(int i=0;i<2;i++){     system.out.println("second statement");     break second; } 

Comments

Popular posts from this blog

css - Which browser returns the correct result for getBoundingClientRect of an SVG element? -

gcc - Calling fftR4() in c from assembly -

.htaccess - Matching full URL in RewriteCond -