静态方法中只允许访问静态数据,那么,如何在静态方法中访问类的实例成员(即没有附加static关键字的字段或方法)?

public class Aa
{
 int x=1;
 static int  y=2;
 public static void method()
//静态方法 { System.out.println("实例变量x = " + new Test().x);
//在静态方法中访问类的实例变量需首先进行类的实例化 System.out.println("静态变量y = " + y);
//在静态方法中可直接访问类的静态变量 } public static void main(String[] args) { Test.method(); Test t = new Test(); System.out.println("x = " + t.x); } }
posted @ 2022-10-07 10:34  叫金克丝爆爆  阅读(15)  评论(0)    收藏  举报