java 如何正确访问静态成员类?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/86607/
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
How do I correctly access static member classes?
提问by fijiaaron
I have two classes, and want to include a static instance of one class inside the other and access the static fields from the second class via the first.
我有两个类,并希望在另一个类中包含一个类的静态实例,并通过第一个类访问第二个类中的静态字段。
This is so I can have non-identical instances with the same name.
这样我就可以拥有同名的不同实例。
Class A
{
public static package1.Foo foo;
}
Class B
{
public static package2.Foo foo;
}
//package1
Foo
{
public final static int bar = 1;
}
// package2
Foo
{
public final static int bar = 2;
}
// usage
assertEquals(A.foo.bar, 1);
assertEquals(B.foo.bar, 2);
This works, but I get a warning "The static field Foo.bar shoudl be accessed in a static way". Can someone explain why this is and offer a "correct" implementation.
这有效,但我收到警告“应该以静态方式访问静态字段 Foo.bar”。有人可以解释为什么会这样并提供“正确”的实现。
I realize I could access the static instances directly, but if you have a long package hierarchy, that gets ugly:
我意识到我可以直接访问静态实例,但是如果你有一个很长的包层次结构,那就很难看:
assertEquals(net.FooCorp.divisions.A.package.Foo.bar, 1);
assertEquals(net.FooCorp.divisions.B.package.Foo.bar, 2);
采纳答案by jon
I agree with others that you're probably thinking about this the wrong way. With that out of the way, this may work for you if you are only accessing static members:
我同意其他人的看法,即您可能以错误的方式思考这个问题。顺便说一句,如果您只访问静态成员,这可能对您有用:
public class A {
public static class Foo extends package1.Foo {}
}
public class B {
public static class Foo extends package2.Foo {}
}
回答by Jason Cohen
You should use:
你应该使用:
Foo.bar
And not:
并不是:
A.foo.bar
That's what the warning means.
这就是警告的意思。
The reason is that barisn't a member of an instanceof Foo. Rather, baris global, on the class Foo. The compiler wants you to reference it globally rather than pretending it's a member of the instance.
其原因是,bar不是一个成员实例的Foo。相反,bar是全局的,在类上Foo。编译器希望您全局引用它,而不是假装它是实例的成员。
回答by axk
There is no sense in putting these two static variables in these to classes as long as you only need to access static members. The compiler expects you to access them trough class name prefixes like:
只要您只需要访问静态成员,将这两个静态变量放在类中是没有意义的。编译器希望您通过类名前缀访问它们,例如:
package1.Foo.bar
package2.Foo.bar
回答by Fernando Barrocal
Once you created the object in:
在以下位置创建对象后:
public static package1.Foo foo;
it isn't being accessed in a Static way. You will have to use the class name and, of course, the full package name to address the class since they have the same name on different packages
它不是以静态方式访问的。您必须使用类名,当然还有完整的包名来寻址类,因为它们在不同的包上具有相同的名称
回答by Kevin Conner
It's true that a Foo instance has access to Foo's static fields, but think about the word "static". It means "statically bound", at least in this case. Since A.foo is of type Foo, "A.foo.bar" is not going to ask the object for "bar", it's going to go straight to the class. That means that even if a subclass has a static field called "bar", and foo is an instance of that subclass, it's going to get Foo.bar, not FooSubclass.bar. Therefore it's a better idea to reference it by the class name, since if you try to take advantage of inheritance you'll shoot yourself in the foot.
Foo 实例确实可以访问 Foo 的静态字段,但请考虑“静态”一词。它的意思是“静态绑定”,至少在这种情况下是这样。由于 A.foo 是 Foo 类型,“A.foo.bar”不会向对象询问“bar”,它会直接进入类。这意味着即使子类有一个名为“bar”的静态字段,并且 foo 是该子类的一个实例,它也会得到 Foo.bar,而不是 FooSubclass.bar。因此,通过类名来引用它是一个更好的主意,因为如果您尝试利用继承,您将在自己的脚下开枪。

