public class JVM1 {
public static void main(String[] args) {
Singleton s = Singleton.getInstantce();
System.out.println(“counter1:” +s.counter1);
System.out.println(“counter2:” +s.counter2);
}
}
class Singleton {
private static Singleton testCase2 = new Singleton();
public static int counter1;
public static int counter2 = 0;
private int counter3 = 4;
private Singleton() {
System.out.println(“counter3:” +counter3);
counter1++;
counter2++;
}
public static Singleton getInstantce() {
return testCase2;
}
}
代码如上
看到代码后我们大多数人不假思索的写出输出结果:
counter3:4
counter1:1
counter2:1
但是结果真的是这样么?
如果你有疑惑可以试试,正确结果应为
counter3:4
counter1:1
counter2:0
我们来简单的分析一下:
JVM类加载机制:加载,连接,初始化。
1、由于Singleton.getInstantce()会主动初始化Singleton类,连接的准备阶段counter1、counter2 会分配内存并设置默认值0,而testCase2则设置为null。
2、当初始化阶段,JVM会由上而下先初始化类变量,及testCase2 会调用new Singleton();创建Singleton实例,此时不会再进行类变量的准备工作,因为类变量保存在方法区,不能重复加载,此时会一次往下进行初始化实例变量counter3,然后直接调用构造函数,counter1和counter2都变成1,将类实例放到堆中。
3、之后初始化counter2,将counter2设置的默认值0赋值给counter2上。
为了验证我们的猜想,我们不妨调整一下类变量的顺序,如下
public class JVM1 {
public static void main(String[] args) {
Singleton s = Singleton.getInstantce();
System.out.println("counter1:"+s.counter1);
System.out.println("counter2:"+s.counter2);
}
}
class Singleton {
private static Singleton testCase2 = new Singleton();
public static int counter1;
public static int counter2 = 0;
private int counter3 = 4;
private Singleton() {
System.out.println(“counter3:” +counter3);
counter1++;
counter2++;
}
public static Singleton getInstantce() {
return testCase2;
}
}
可以试试输出结果为:
counter1:1
counter2:1