3

Why there cannot be multiple exit points from a static initializer? Does Java Language specification state so?

When trying to compile code as:

class HelloWorldApp {
    static {
        if(1 > 2)
          return;
        System.out.println("static"); 
    }

    public static void main(String[] args) {
        System.out.println("Hello World!"); 
    }
}

Compiler prints out an error: return outside method

Java disassembly with javap shows that static is a void method, so would it be possible, theoretically, to create a bytecode that would have multiple 'returns'?

10
  • 3
    The JLS states that a return statement within a static initializer is illegal here. Commented Sep 24, 2014 at 16:14
  • This looks like a JVM question, not a JLS one. The too specs do differ at times, and this may be one of them. Commented Sep 24, 2014 at 16:15
  • see at : stackoverflow.com/questions/11118226/… Commented Sep 24, 2014 at 16:17
  • @SotiriosDelimanolis turn that into an answer, so I can accept it Commented Sep 24, 2014 at 16:18
  • 1
    @ChrisK I was able to create the .class file with Jasmin, thanks, so it runs in JVM without problems. That makes me wondering why would the return statement be forbidden in the JLS. Commented Sep 24, 2014 at 16:24

1 Answer 1

5

The JLS states that a return statement within a static initializer is illegal here.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.