JLS 中是否对静态初始化块的执行顺序有任何保证?

发布于 2024-09-05 13:32:55 字数 758 浏览 7 评论 0原文

我想知道使用如下结构是否可靠:

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);
}

是否有可能得到 NullPointerException 因为 msgSource 初始化块将在初始化 的块之前执行engMessages?

(关于为什么我不在上层 init.block 的末尾进行 msgSource 初始化:这只是品味问题;如果所描述的构造不可靠,我会这样做)

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)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

薄情伤 2024-09-12 13:32:55

是的,静态初始化块保证按文本顺序执行。

来自 JLS,第 12.4 节.1

目的是类或接口类型具有一组初始值设定项,将其置于一致状态,并且该状态是其他类观察到的第一个状态。 静态初始化器和类变量初始化器按文本顺序执行,并且可能不会引用在使用后以文本形式出现的类中声明的类变量,即使这些类变量在作用域内(第 8.3 节) .3).此限制旨在在编译时检测大多数循环或其他格式错误的初始化。

并来自 12.4.2< /a>:

接下来,执行类变量初始值设定项和类的静态初始值设定项,或者接口的字段初始值设定项,按文本顺序,就好像它们是单个块一样。

但就我个人而言,我会将所有变量声明放在开头,然后放置一个静态初始化块。我认为这更容易遵循。

Yes, static initializer blocks are guaranteed to execute in textual order.

From the JLS, section 12.4.1:

The intent is that a class or interface type has a set of initializers that put it in a consistent state, and that this state is the first state that is observed by other classes. The static initializers and class variable initializers are executed in textual order, and may not refer to class variables declared in the class whose declarations appear textually after the use, even though these class variables are in scope (§8.3.3). This restriction is designed to detect, at compile time, most circular or otherwise malformed initializations.

And from 12.4.2:

Next, execute either the class variable initializers and static initializers of the class, or the field initializers of the interface, in textual order, as though they were a single block.

Personally though, I'd put all the variable declarations at the start, and then a single static initializer block. I consider that to be a lot easier to follow.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文