静态方法中只允许访问静态数据,那么,如何在静态方法中访问类的实例成员(即没有附加static关键字的字段或方法)?
代码:
package test;
public class jingtaihanshu
{
int x = 3;
static int y = 4;
public static void Method()//静态方法
{
System.out.println("实例变量x = " + new jingtaihanshu().x);
System.out.println("静态变量y = " + y);
}
public static void main(String[] args)
{jingtaihanshu.Method();
jingtaihanshu ex = new jingtaihanshu();
System.out.println("x = " + ex.x);
}
}
结果:
标签:02,静态方法,对象,System,static,有趣,jingtaihanshu,public,out From: https://www.cnblogs.com/xlm926/p/16885141.html