1 public class MyTest5 { 2 3 public static void main(String[] args) { 4 Singleton singleton = Singleton.getInstance(); 5 6 System.out.println("counter1: "+Singleton.counter1); 7 System.out.println("counter2: "+Singleton.counter2); 8 } 9 } 10 11 class Singleton { 12 public static int counter1; 13 14 public static int counter2 = 0; 15 16 private static Singleton singleton = new Singleton(); 17 18 private Singleton(){ 19 counter1++; 20 counter2++; 21 } 22 23 public static Singleton getInstance(){ 24 return singleton; 25 } 26 } 27 28 /* 29 * 输出: 30 * counter1: 1 31 * counter2: 1 32 * */
1 public class MyTest5 { 2 3 public static void main(String[] args) { 4 Singleton singleton = Singleton.getInstance(); 5 6 System.out.println("counter1: "+Singleton.counter1); 7 System.out.println("counter2: "+Singleton.counter2); 8 } 9 } 10 11 class Singleton { 12 public static int counter1; 13 14 private static Singleton singleton = new Singleton(); 15 16 private Singleton(){ 17 counter1++; 18 counter2++; 19 } 20 21 public static int counter2 = 0; 22 23 public static Singleton getInstance(){ 24 return singleton; 25 } 26 } 27 28 /* 29 * 输出: 30 * counter1: 1 31 * counter2: 0 32 * */
3.JVM的接口初始化规则与类加载器准备阶段和初始化阶段的重要意义分析
原文:https://www.cnblogs.com/zhihaospace/p/12293193.html