Java 中的全局变量,可以在包中的任何类中访问它

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9689574/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-16 06:11:03  来源:igfitidea点击:

Global variable in Java with access to it in any class in a package

java

提问by Fernando á.

I have a package with different classes. One class implements a structure data. I want to use an instance of this class as a global variable in all the classes of my package. How can I use and modify this variable, remaining its content.

我有一个不同类的包。一个类实现结构数据。我想在我的包的所有类中使用这个类的一个实例作为全局变量。如何使用和修改此变量,保留其内容。

Creating a static variable is not what I need, because I need to modify it, not just read it.

创建静态变量不是我需要的,因为我需要修改它,而不仅仅是读取它。

采纳答案by Jason S

something like this?

像这样的东西?

class MyMetadata
{
    private int x;
    private int y;
    public int getX() { return this.x; }
    public int getY() { return this.y; }
    public void setX(int newX) { this.x = x; }
    public void setY(int newY) { this.y = y; }
    MyMetadata(int x, int y) { this.x = x; this.y = y; }

    static final private MyMetadata INSTANCE = new MyMetadata(0,0);
    static public MyMetadata getInstance() { return INSTANCE; }
}

// in client code:
int needsX = MyMetadata.getInstance().getX();

Just be sure to handle concurrency issues carefully.

请务必小心处理并发问题。

回答by Jigar Joshi

You can always modify the content of static variable(its final which you can't modify),

您可以随时修改静态变量的内容(您无法修改的最终变量),

It would be nice if you wrap up all the data into a singleton instance and share that instance

如果您将所有数据打包到一个单例实例中并共享该实例,那就太好了

回答by Jigar Joshi

There is nothing that says a staticvariable can't be reassigned to. "static" in this case just means it is not associated with an instance.

没有什么说static变量不能重新分配的。在这种情况下,“静态”仅表示它与实例无关

On the other hand, the finalmodifier prevents a variable from being re-assigned to.

另一方面,final修饰符防止变量被重新分配。

That being said, I would highly encourage trying to avoid the use of a [writeable] static variable. Generally I find this can be encapsulated as some sort of state instance: singleton (ick!) or otherwise.

话虽如此,我强烈鼓励尝试避免使用 [可写] 静态变量。通常我发现这可以封装为某种状态实例:单例(ick!)或其他。

Happy coding.

快乐编码。

回答by Java42

Remove the 'final' keyword from the static and then you can modify it.

从静态中删除“final”关键字,然后您可以对其进行修改。

static int globalInt = -42;

Can be used and/or updated anywhere.

可以在任何地方使用和/或更新。

回答by raddykrish

The only way to share instance across application is by Singleton mechanism. But its becoming an anti pattern recently as it makes hard to maintain and also test such applications. There are lots of explanation on that why its an anti pattern on the web.

跨应用程序共享实例的唯一方法是通过单例机制。但它最近成为一种反模式,因为它很难维护和测试此类应用程序。关于为什么它是网络上的反模式有很多解释。

As told by others static variables can be modified, finals are the ones which cannot be.

正如其他人所说的静态变量可以修改,finals 是不能修改的。