一个类的其他实例是否能够访问另一个类中的静态变量?
我有一个类,我知道它的每个实例都会由 URLClassLoader 加载,因此,如果我在一个类中有一个静态变量,其他实例是否能够访问它?
例如,Class MyClass 由 ClassLoader A 和 ClassLoader B 加载,我想知道 A 加载的 MyClass 是否与 B 加载的 MyClass 具有相同的静态字段。
所以基本上,以下语句始终为真:
A.loadClass("MyClass").getField("MyField").get(null).equals(B.loadClass("MyClass").getField("MyField").get(null));
I have a class that I know will be loaded by a URLClassLoader for each instance of it, so if I have a static variable in one, will the other instances be able to access it?
For example, Class MyClass is loaded by ClassLoader A and ClassLoader B, and I want to know if MyClass loaded by A will have the same static fields as MyClass loaded by B.
So basically, will the following statement always be true:
A.loadClass("MyClass").getField("MyField").get(null).equals(B.loadClass("MyClass").getField("MyField").get(null));
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
静态变量在类加载器中是唯一的。 (对这些的引用是)值可能是相同的。使用 equals 可以,使用 == 不行。
编辑:每个类加载器将加载不同的类(它们甚至可以是不同的版本)。初始化的静态数据将会不同。来自不同类加载器的两个不同类可以具有相同值的变量(静态或非静态)。
Static variables are unique across classloaders. (The references to these that is) The values might be the same. Using equals will work, using == will not.
EDIT: Each ClassLoader will load a different class (They can even be different versions). The initialized statics will be different. Two different classes from different ClassLoaders can have variables (static or not) that are identical in value.
不幸的是@Dinesh/@DAJ 的答案是不正确的。 (@Romain 也可能是这样,但是措辞很难解析。)
假设您有一个类
abC
并且您安排同一个类由两个不同的类加载器加载。根据规范,您最终将得到两个不同的Class
对象,其完全限定名称为abC
,从类型系统的角度来看,这是两个不同的类型。每种类型都有一组不同的静态变量。主要参考是 JLS 4.3.4< /a> - 第 2 和第 3 段。
您可以推断出每个不同的引用类型(在运行时)将具有来自 JLS 4.12.3,JLS 8.3.1.1, JLS 12.4 以及语言规范的其他部分。
一般情况下不会。
在某些情况下它总是正确的,具体取决于
MyClass
如何初始化myField
。例如,如果该字段被初始化为文字字符串,那么它就会被初始化。(观察这一点的技巧是安排
MyClass
实际上由两个类加载器A
和B
加载,而不是由共同的祖先类加载器。)Unfortunately @Dinesh/@DAJ's answer is incorrect. (@Romain's maybe too, but the wording is hard to parse.)
Suppose you have a class
a.b.C
and you arrange that the same class gets loaded by two different classloaders. According to the specifications, you will end up with two distinctClass
objects with the fully qualified namea.b.C
, and from the type system perspective two distinct types. Each of the types will have a different set of static variables.The primary reference for this is JLS 4.3.4 - paragraphs 2 and 3.
You can infer that each reference type that is distinct (at runtime) will have a distinct set of statics from JLS 4.12.3, JLS 8.3.1.1, JLS 12.4, and other parts of the language spec.
In general it won't.
It will always be true in some cases, depending on how
MyClass
initializesmyField
. For instance, if the field is initialized to a literal String, then it will.(The trick to observing this is to arrange that
MyClass
is actually loaded by the two classloadersA
andB
, and not by a common ancestor classloader.)AFAIK,谁加载该类并不重要。只要字段是静态的,它们就应该是相同的。您是否尝试过使用一些代码进行实验?
AFAIK, it shouldn't matter who loads the class. As long as the fields are static, they should be the same. Did you try to experiment with some code?