[英]static member being accessed by instance reference
I have a call to a function and the background of that call is yellow and it says "static member being accessed by instance reference," but it works perfectly without errors.我有一个函数调用,该调用的背景是黄色的,它说“静态成员正在通过实例引用访问”,但它可以完美地工作而没有错误。
Should I have to solve that somehow or is it okay?我应该以某种方式解决这个问题还是可以?
Here is a code sample:这是一个代码示例:
class A {
static int x = 2;
...
}
Instantiation is some other file:实例化是其他一些文件:
A a = new A();
a.x;
This warning happens when you have something like this:当您遇到以下情况时会发生此警告:
class A {
static int x = 2;
}
...
A a = new A();
a.x; // accessing static member by instance
You should access the static member x
via the class (or interface) instead:您应该通过类(或接口)访问静态成员x
:
A a = new A();
A.x;
Static members belong to the class, not to a particular instance.静态成员属于类,而不属于特定实例。
如果只需要方法输出,可以这样:
int x = A.x
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.