Returning in a static initializer

Posted by Martijn Courteaux on Stack Overflow See other posts from Stack Overflow or by Martijn Courteaux
Published on 2010-04-09T11:53:29Z Indexed on 2010/04/09 12:13 UTC
Read the original article Hit count: 346

Hello,

This isn't valid code:

public class MyClass
{
    private static boolean yesNo = false;

    static
    {
        if (yesNo)
        {
            System.out.println("Yes");
            return; // The return statement is the problem
        }
        System.exit(0);
    }
}

This is a stupid example, but in a static class constructor we can't return;. Why? Are there good reasons for this? Does someone know something more about this?

So the reason why I should do return is to end constructing there.

Thanks

© Stack Overflow or respective owner

Related posts about java

Related posts about class-constructors