Java中静态字段的确切含义是什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/797964/
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
What is the exact meaning of static fields in Java?
提问by LB40
I would like to share an object between various instances of objects of the same class.
我想在同一类的各种对象实例之间共享一个对象。
Conceptually, while my program is running, all the objects of class A access the same object of class B.
从概念上讲,当我的程序运行时,A 类的所有对象都访问 B 类的同一个对象。
I've seen that static
is system-wide and that its usage is discouraged. Does that mean that if I've got another program running on the same JVM that instantiates objects of class A, these objects could potentially access the same B object as the one accessed in the previous program?
我已经看到这static
是系统范围的,并且不鼓励使用它。这是否意味着如果我在同一个 JVM 上运行另一个程序来实例化 A 类的对象,这些对象可能会访问与前一个程序中访问的对象相同的 B 对象?
What are generally the flaws behind using static fields?
使用静态字段背后的缺陷通常是什么?
Are there any alternatives (that do not require a huge effort of implementation)?
是否有任何替代方案(不需要大量的实施工作)?
采纳答案by Jon Skeet
Static doesn't quite mean "shared by all instances" - it means "not related to a particular instance at all". In other words, you could get at the static field in class A without ever creating anyinstances.
静态并不完全意味着“由所有实例共享”——它意味着“与特定实例完全无关”。换句话说,您可以在不创建任何实例的情况下获取类 A 中的静态字段。
As for running two programs within the same JVM - it really depends on exactly what you mean by "running two programs". The static field is effectivelyassociated with the class object, which is in turn associated with a classloader. So if these two programs use separate classloader instances, you'll have two independent static variables. If they both use the same classloader, then there'll only be one so they'll see each other's changes.
至于在同一个 JVM 中运行两个程序 - 这实际上取决于“运行两个程序”的确切含义。静态字段有效地与类对象相关联,而类对象又与类加载器相关联。因此,如果这两个程序使用单独的类加载器实例,您将拥有两个独立的静态变量。如果他们都使用相同的类加载器,那么只会有一个,所以他们会看到彼此的变化。
As for an alternative - there are various options. One is to pass the reference to the "shared" object to the constructor of each object you create which needs it. It will then need to store that reference for later. This can be a bit of a pain and suck up a bit more memory than a static approach, but it does make for easy testability.
至于替代方案 - 有多种选择。一种是将对“共享”对象的引用传递给您创建的每个需要它的对象的构造函数。然后它需要存储该引用以备后用。与静态方法相比,这可能有点麻烦,并且会占用更多的内存,但它确实易于测试。
回答by Pesto
Static methods and members are discouraged because they're so frequently misused, but this sounds like a situation where static is the correct way to go. As to static shared across multiple programs, this is not the case. Each program runs in a completely separate environment.
不鼓励使用静态方法和成员,因为它们经常被误用,但这听起来像是使用静态方法的正确方法。至于跨多个程序共享的静态,情况并非如此。每个程序都在完全独立的环境中运行。
回答by Paul Tomblin
What you are looking for is called the Singleton Pattern.
您正在寻找的称为Singleton Pattern。
回答by A_M
Assuming everything is in the same class loader, then why not use the monostatepattern to do this?
假设一切都在同一个类加载器中,那么为什么不使用单态模式来做到这一点呢?
Your shared static is hidden in the monostate:
您的共享静态隐藏在单态中:
public class Monostate { private static String str = "Default"; public String getString() { return str; } public void setString(String s) { str = s; } }
Then you are free to create as many instances of the monostate as you like, but they all share the same underlying object due to the static reference.
然后您可以随意创建任意数量的单态实例,但由于静态引用,它们都共享相同的底层对象。
Monostate mono = new Monostate(); mono.setString("Fred"); System.out.println(mono.getString());