我应该为主程序变量使用静态变量吗?

时间:2021-10-06 06:55:10

I have read posts on this, so I'm aware about how static variables should be avoided, they are not object oriented, they're like globals etc.

我已经阅读了这篇文章,所以我知道应该如何避免静态变量,它们不是面向对象的,它们就像全局变量等。

But here's my question, hopefully it's not a repeat: I have some private class variables that many methods use and passing around would be tedious. The class I'm talking about is my main program, so the only instance of it will be the instance that the JVM creates.

但这是我的问题,希望它不是重复:我有一些私有类变量,许多方法使用和传递将是乏味的。我正在谈论的类是我的主程序,因此它的唯一实例将是JVM创建的实例。

In this case does it make any difference if these variables are static or not ? (Perhaps if a user opens my application multiple times and I make the variables static, they would share the variables and mix each other up?)

在这种情况下,如果这些变量是静态的,它会有什么不同吗? (也许如果用户多次打开我的应用程序并且我将变量设置为静态,他们会共享变量并相互混合?)

I'd like to use statics because want to access these variables from inside enums. Thanks

我想使用静态,因为想要从枚举内部访问这些变量。谢谢

Here's the code for the enum part:

这是枚举部分的代码:

enum Buttons {

    OPEN_BUTTON("Open file...",false),
    CHANGE_FONT_BUTTON("Change font",false),
    DECOR_BUTTON("Decor font",true),
    EDITOR_BUTTON("Open editor",false),
    ALPHABET_BUTTON("Open alphabet browser",false),
    CTEST_BUTTON("Start consonant test",false),
    TESTTYPE_BUTTON("Select test type...",false),
    TEST_BUTTON("Start word test",false),
    QUIT_BUTTON("Quit", false);

    private ButtonBase button;

    Buttons(String title, boolean toggle) {
        if (toggle) button = ToggleButtonBuilder.create().prefWidth(200).prefHeight(35).text(title).build();
        else button = ButtonBuilder.create().prefWidth(200).prefHeight(35).text(title).build();}

    void onClick(EventHandler<MouseEvent> eh) {button.setOnMouseClicked(eh);}
    ButtonBase getBase() {return button;}
    boolean toggled() { return ((ToggleButton)button).isSelected();  }
    void setToggle(boolean on) {((ToggleButton) button).setSelected(on); }
    void enable() {button.setDisable(false);}
    void disable() {button.setDisable(true);}
    void setText(String text) { button.setText(text);}
    void clicked() {

// this is where i'd like to have the event handlers...

//这是我想拥有事件处理程序的地方......

3 个解决方案

#1


2  

If you find you're passing teh same data around a lot then it seems to me like that data may have a certain amount of correlation - perhaps by encapsulating that data into some kind of value object you wouldnt need to use effectively global data?

如果你发现你经常传递相同的数据,那么在我看来,数据可能具有一定的相关性 - 也许通过将数据封装到某种价值对象中,你不需要有效地使用全局数据?

#2


4  

You state:

你说:

I'm using JavaFx and I'm making my Buttons into an enum, and I'd like to add the ActionEvent (click) handler inside the enum, so it's all neatly in one place. The handlers need to access the class variables.

我正在使用JavaFx,我正在将我的按钮变成一个枚举,我想在枚举中添加ActionEvent(click)处理程序,所以它在一个地方整齐地排列。处理程序需要访问类变量。

This is not a good design, is not a reason for static variables, and also a mis-use of enums. You're tossing out all benefit of OOP with this design and so I urge you to just not do this. Note that if/when you update or modify your code, you will often change your GUI components, and enums should (almost) never change. They should represent the unchanging bedrock of your logic. They should not be used for objects whose state changes as your buttons currently do.

这不是一个好的设计,不是静态变量的原因,也是枚举的误用。你正在通过这种设计抛出OOP的所有好处,所以我敦促你不要这样做。请注意,如果/当您更新或修改代码时,通常会更改GUI组件,并且枚举应该(几乎)永远不会更改。它们应该代表你逻辑的不变基石。它们不应该用于状态随按钮当前变化的对象。

I would also recommend against singleton pattern here as there is no need to use this for GUI components (and in fact singletons are quite similar to enums). Why not simply create a View class?

我也建议在这里使用单例模式,因为不需要将它用于GUI组件(事实上,单例与枚举非常相似)。为什么不简单地创建一个View类?

#3


-1  

Within a single classloader, static fields are always shared. To explicitly scope data to threads, you'd want to use a facility like ThreadLocal.

在单个类加载器中,静态字段始终是共享的。要将数据显式地定位到线程,您需要使用类似ThreadLocal的工具。

Link

链接

So if you use statics with ThreadLocal, you won't have a problem with multiple instances of your app.

因此,如果您在ThreadLocal中使用静态,那么您的应用的多个实例就不会有问题。

#1


2  

If you find you're passing teh same data around a lot then it seems to me like that data may have a certain amount of correlation - perhaps by encapsulating that data into some kind of value object you wouldnt need to use effectively global data?

如果你发现你经常传递相同的数据,那么在我看来,数据可能具有一定的相关性 - 也许通过将数据封装到某种价值对象中,你不需要有效地使用全局数据?

#2


4  

You state:

你说:

I'm using JavaFx and I'm making my Buttons into an enum, and I'd like to add the ActionEvent (click) handler inside the enum, so it's all neatly in one place. The handlers need to access the class variables.

我正在使用JavaFx,我正在将我的按钮变成一个枚举,我想在枚举中添加ActionEvent(click)处理程序,所以它在一个地方整齐地排列。处理程序需要访问类变量。

This is not a good design, is not a reason for static variables, and also a mis-use of enums. You're tossing out all benefit of OOP with this design and so I urge you to just not do this. Note that if/when you update or modify your code, you will often change your GUI components, and enums should (almost) never change. They should represent the unchanging bedrock of your logic. They should not be used for objects whose state changes as your buttons currently do.

这不是一个好的设计,不是静态变量的原因,也是枚举的误用。你正在通过这种设计抛出OOP的所有好处,所以我敦促你不要这样做。请注意,如果/当您更新或修改代码时,通常会更改GUI组件,并且枚举应该(几乎)永远不会更改。它们应该代表你逻辑的不变基石。它们不应该用于状态随按钮当前变化的对象。

I would also recommend against singleton pattern here as there is no need to use this for GUI components (and in fact singletons are quite similar to enums). Why not simply create a View class?

我也建议在这里使用单例模式,因为不需要将它用于GUI组件(事实上,单例与枚举非常相似)。为什么不简单地创建一个View类?

#3


-1  

Within a single classloader, static fields are always shared. To explicitly scope data to threads, you'd want to use a facility like ThreadLocal.

在单个类加载器中,静态字段始终是共享的。要将数据显式地定位到线程,您需要使用类似ThreadLocal的工具。

Link

链接

So if you use statics with ThreadLocal, you won't have a problem with multiple instances of your app.

因此,如果您在ThreadLocal中使用静态,那么您的应用的多个实例就不会有问题。