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

 1 public class Test3 {
 2     //类的实例变量
 3     int x=1;
 4     //类的静态变量初始值为2
 5     static int y=2; //类的静态变量初始值为2
 6     //静态方法
 7     public static void out() {  
 8         System.out.println("类的实例化变量x:"+new Test3().x);  //必须通过类的实例化来访问x 的值
 9         System.out.println("类的静态变量y:"+y);
10     }
11     public static void main(String[] args) {
12         Test3.out();
13         Test3 t=new Test3();
14         System.out.println("x="+t.x);
15         System.out.println("y="+t.y);
16     }
17 
18 }

运行结果如下:

类的实例化变量x:1
类的静态变量y:2
x=1
y=2

posted @ 2019-10-16 13:02  不懂就要问!  阅读(144)  评论(0编辑  收藏  举报