How to use labels in java code?

break with a label is not the same as a goto statement. Java does not have a goto statement.

A label marks the statement that follows it. You can use it to break out of that statement, and only out of that statement. Control of flow will always transfer to the end of the labeled statement.

So what do you have here?

        label149: if (!localIterator2.hasNext());

Because of the semicolon after the if, this is in fact the entire labeled statement. So your break label149 is not within its scope at all. If you did not have that semicolon, the if block would include the while, and then the break label149 would work. But control would be transferred to the line after the end of the while block.

       label246: ((HashMap)Saved.this.keynamedata.get(i)).put("is_paid", "0");

This is the statement marked by label246. Again, the break label246 is not inside it, so it is not in its scope, and you can`t break out of a statement you are not inside of.

Leave a Comment