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

*爱你&永不变心* 提交于 2019-12-01 12:10:57
 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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!