0
我知道静态变量是类的一部分,而不是对象的一部分。如何代码工作的以下行没有任何问题Java中的静态变量
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对象访问我的变量?
所有实例变量共享该静态变量。你引用了它不正确,但它仍然运行 –
http://stackoverflow.com/questions/17242649/can-non-static-methods-modify-static-variables –