简体   繁体   English

实例引用访问的静态成员

[英]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.

相关问题 通过实例引用访问静态成员的最佳解决方案 - Best solution for static member being accessed by instance reference (Java) Static 成员通过枚举器的实例引用访问 - (Java) Static member accessed via instance reference with enumerators 通过实例引用访问的静态成员(使用“this”关键字) - Static member accessed via instance reference (using 'this' keyword) 通过实例引用访问静态成员'android.content.Context.MODE_PRIVATE' - Static member 'android.content.Context.MODE_PRIVATE' accessed via instance reference 与.Net不同,为什么Java中的实例可以访问静态成员 - Unlike .Net Why the static member can be accessed by the instance in Java Java家庭作业帮助(通过实例参考访问静态成员) - Java Homework Help (Accessing Static Member via Instance Reference) 需要代码来理解同步的 static 和同一实例上的两个不同线程同时访问的非静态方法 - Need code to understand synchronized static and non-static methods being accessed at the same time by two different threads on the same instance 访问数组成员时抛出NullPointerException - NullPointerException being thrown whenever array member is accessed 如何在main()中访问私有静态实例变量 - how private static instance variable is accessed in main() 为什么无法从枚举构造函数的 lambda 访问实例成员? - Why can't the instance member be accessed from the lambda of the enum constructor?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM