`
m635674608
  • 浏览: 4937005 次
  • 性别: Icon_minigender_1
  • 来自: 南京
社区版块
存档分类
最新评论

cinit init

 
阅读更多
public class Test {
	private static Test tester = new Test(); // step 1
	private static int count1; // step 2
	private static int count2 = 0; // step 3

	public Test() { // step 4
		count1++;
		count2++;
		}
public static Test getTester() { // step 5
		return tester;
	}

	public static void main(String[] args) {
		Test.getTester();
		 System.out.println("" + count1 + count2);
	}
}

 JAVA类首次装入时,开始加载静态变量和静态块,也就是说会首先为静态区域分配内存空间,此时tester、count1、count2都已经分配空间,其中tester为一个引用空间,count1、count2为默认值0。
第二步开始执行  private static Test tester = new Test()  这段代码,调用构造器打印出count1、count2  分别为 1 和 1 。然后依次执行一下代码  private static int count1;    private static int count2 = 0;  此时,count2被重置为0,因此如果此时再次打印的话count1、count2的值应该为 1 和 0 。
估计是楼主的题目记忆有问题,这题考察的重点应该在于count被重置后的结果,代码应该不是这样的

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics