public class test
{
int a=10;
pulblic static void main(String [] args)
{
test test=new test();
System.out.print(test.a);
}
}
/*我在静态方法里对对象进行了操作了,还访问了他了实例域。我的理解不知道是怎么回事,肯请大师指点!!!
谢谢!!!*/
{
int a=10;
pulblic static void main(String [] args)
{
test test=new test();
System.out.print(test.a);
}
}
/*我在静态方法里对对象进行了操作了,还访问了他了实例域。我的理解不知道是怎么回事,肯请大师指点!!!
谢谢!!!*/
所谓“静态方法不能直接访问非静态成员”,意思是指,你不可以写这种代码:
public class test
{
int a=10;
pulblic static void main(String [] args)
{
System.out.print(a);
}
}
静态method被调用时没有传入this指针作为参数,所以就无法访问this.a, 但是在你的代码中访问的是test.a,因为test所引用的Object你已经创建了,所以你当然就可以访问test.a了。所以理解这个问题的关键就是this指针的问题