我知道静态变量是类的一部分,而不是 Object 的一部分。下面这几行代码如何运行没有问题
class M
{
static int i=0;
void Inc()
{
System.out.println("Global "+M.i);
System.out.println("Local "+this.i);
}
}
public class StaticTest
{
public static void main(String args[])
{
M m1=new M();
m1.i=99; //How can the m1 object access i variable of the class
m1.Inc();
}
}
我得到的输出是
Global 99
Local 99
m1对象如何访问类的i变量?
最佳答案
在这两种情况下,它是完全相同的i变量。
不幸的是,java允许您使用非静态
语法访问静态字段。
这就是全部内容,没有其他内容。
关于java - Java 中的静态变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43827952/