Skip to main content
edited tags
Link
Bergi
  • 662.4k
  • 158
  • 1k
  • 1.5k
Source Link
szymond
  • 1.3k
  • 2
  • 20
  • 41

using labels in java without "loops"

I always thought that the labels must be used only with loops but it seems not. Giving such code:

public class LabelTest {
    public static void main(String[] args) {
        label1: System.out.println("");
        label2: LabelTest t = new LabelTest();  
    }                                               
}

When compiled line labeled "label1" compiles but the code at "label2" gives errors. Why's that? And why would I want to label statements which are not "loops"?