如何在静态方法中访问类的实例成员

如何在静态方法中访问类的实例成员

静态方法中不能直接访问同一类中的非静态成员,而必须先创建对象,再通过对象访问成员。

例如:

public class Ex
{
int i =123;
static int Temp;

public static void A()
{
System.out.println(Temp);
}

public int B()
{
int x = 100;

return x;
}

public static void main(String args[])
{
Ex e = new Ex();
Temp = e.B();
e.A();
}
}
posted @ 2015-10-13 18:10  梦玄庭  阅读(438)  评论(0编辑  收藏  举报