2

Possible Duplicate:
Returning in a static initializer

Is there a way to exit a static initialiser in Java, something like the code below (which does not compile):

public class Test {

    private static int i = 1;

    static {
        if (i == 0) {
            return; // DOESN'T COMPILE
        }
        i = 0;
    }
}

ps: yes I know, the example makes no sense, i == 0 will always be false at this point, but that's not the point!

Community
  • 1
  • 1
assylias
  • 321,522
  • 82
  • 660
  • 783

1 Answers1

2

A quick and dirty method would be, misusing a for loop and use its break statement for flow control:

static
{
    int i = 0;

    for(;;)
    {
        if(i == 0)
            break;
        // more code
        // more conditions
        // don't forget the final break
        break;
    }
}
Neet
  • 3,937
  • 15
  • 18