Swing 中静态成员的顺序是否敏感?

发布于 2024-11-10 06:22:18 字数 1479 浏览 5 评论 0原文

我有这个类,比方说,Foo。它扩展了 JFrame 并且是一个单例。也就是说,它有两个静态字段:1) Foo 的实例和 2) Color

这是Foo的代码片段:

public class Foo extends JFrame{
    private static final Color FOO_RED = new Color(155, 64, 69);
    private static final Foo INSTANCE = new Foo();

    private Foo(){
        //do stuff
    }

    public static Foo getInstance(){
        return INSTANCE;
    }
}

我还有另一个类,比如说Launcher。这是负责启动应用程序的主类。它是一个简单的类,其唯一的工作是将构造 Foo 的任务委托给 EDT

这是Launcher的代码片段:

public class Launcher{
    public static void main(String[] args){
        SwingUtilities.invokeLater((new Runnable(){
            @Override
            public void run()
            {
                Foo.getInstance();
            }
        }));
    }
}

现在,这一切都工作得很好。 但是,当我切换 Foo 字段的顺序(参见下文)时,使用 FOO_RED 的组件不再涂成这种颜色。

public class Foo extends JFrame{
    private static final Foo INSTANCE = new Foo(); //declared before Color
    private static final Color FOO_RED = new Color(155, 64, 69);

    private Foo(){
        //do stuff
    }

    public static Foo getInstance(){
        return INSTANCE;
    }
}

因此,这就引出了一个问题,对于 Swing 来说,静态字段的顺序重要吗?

I have this class, let's say, Foo. It extends JFrame and is a singleton. That being said, it has two static fields: 1) an instance of Foo and 2) a Color.

Here's a code snippet of Foo:

public class Foo extends JFrame{
    private static final Color FOO_RED = new Color(155, 64, 69);
    private static final Foo INSTANCE = new Foo();

    private Foo(){
        //do stuff
    }

    public static Foo getInstance(){
        return INSTANCE;
    }
}

I also have another class, let's say, Launcher. This is the main class that is responsible for launching the application. It's a simple class in that its only job is to delegate the task of constructing Foo to the EDT.

Here's a code snippet of Launcher:

public class Launcher{
    public static void main(String[] args){
        SwingUtilities.invokeLater((new Runnable(){
            @Override
            public void run()
            {
                Foo.getInstance();
            }
        }));
    }
}

Now, this all works just fine. But, when I switch the ordering of Foo's fields (See below), the components that use FOO_RED are no longer painted this color.

public class Foo extends JFrame{
    private static final Foo INSTANCE = new Foo(); //declared before Color
    private static final Color FOO_RED = new Color(155, 64, 69);

    private Foo(){
        //do stuff
    }

    public static Foo getInstance(){
        return INSTANCE;
    }
}

So, this begs the question, does the ordering of static fields matter when it comes to Swing?

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

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

发布评论

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

评论(3

爱的故事 2024-11-17 06:22:18

正如已经提到的,静态字段的顺序确实很重要。按照它们出现的顺序执行。

我将对这个例子进行另一个更改。这将使静态字段顺序变得不那么重要。

更新:使用 IDOH(按需持有者初始化) 模式使单例线程安全。

private static class FooHolder {
    private static final Foo INSTANCE = new Foo();
}

public static Foo getInstance(){
    return FooHolder.INSTANCE;
}

As already mentioned, ordering of static fields does matter. Executed in the order they appear.

I would make another change to this example. This would make you static field order less important.

UPDATE: Use IDOH (Initialization on Demand Holder) pattern to make singleton thread safe.

private static class FooHolder {
    private static final Foo INSTANCE = new Foo();
}

public static Foo getInstance(){
    return FooHolder.INSTANCE;
}
我乃一代侩神 2024-11-17 06:22:18

是的。对于任何静态字段/块来说,排序都很重要。


编辑
首先,静态字段被设置为其默认值(因此 FOO_REDnull)。然后静态字段按照其出现的顺序进行初始化。因此可以在初始化之前观察 FOO_RED 字段。

Yes. ordering matters for any static fields/blocks.


EDIT:
First, static fields are set to their default value(So FOO_RED is null).then the static fields are initialized in the order it appears. so it is possible to observe the FOO_RED field before it is initialized.

妞丶爷亲个 2024-11-17 06:22:18

顺序对于静态字段确实很重要,因为它们是从上到下初始化的。在您的情况下,切换顺序会导致 Foo() 构造函数在 FOO_RED 初始化之前被调用。因此,在构造函数中使用时,FOO_RED 将为 null。这是合法的,但显然对任何想要使用 FOO_RED 的控件没有用处。

The ordering does matter for static fields, as they are initialized from top to bottom. In your case, switching the order causes the Foo() constructor to be called before the initialization of FOO_RED. As such, FOO_RED will be null for use within the constructor. This is legal, though obviously of no use to any controls that would like to use FOO_RED.

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