Are there any guarantees in JLS about order of execution static initialization blocks?

Posted by Roman on Stack Overflow See other posts from Stack Overflow or by Roman
Published on 2010-06-12T09:51:12Z Indexed on 2010/06/12 10:12 UTC
Read the original article Hit count: 285

Filed under:
|
|

I wonder if it's reliable to use a construction like:

private static final Map<String, String> engMessages;
private static final Map<String, String> rusMessages;

static {
    engMessages = new HashMap<String, String> () {{
        put ("msgname", "value");
    }};
    rusMessages = new HashMap<String, String> () {{
        put ("msgname", "????????");
    }};
}

private static Map<String, String> msgSource;

static {
    msgSource = engMessages;
}

public static String msg (String msgName) {
    return msgSource.get (msgName);
}

Is there a possibility that I'll get NullPointerException because msgSource initialization block will be executed before the block which initializes engMessages?

(about why don't I do msgSource initialization at the end of upper init. block: just the matter of taste; I'll do so if the described construction is unreliable)

© Stack Overflow or respective owner

Related posts about java

Related posts about static-initialization