package com.ygl;标签:SingleTon,java,虚拟机,singleTon,counter1,深入,static,public,counter2 From: https://blog.51cto.com/u_10028442/5824678
class SingleTon{
private static SingleTon singleTon=new SingleTon();
public static int counter1;
public static int counter2=0;
public SingleTon(){
counter1++;
counter2++;
}
public static SingleTon getInstance(){
return singleTon;
}
}
public class MyTest {
public static void main(String[] args) {
SingleTon singleTon=SingleTon.getInstance();//触发类加载,先为静态变量分配内存(以及默认初始值,执行=左边),后赋值,执行=右边
System.out.println("counter1="+singleTon.counter1);
System.out.println("counter1="+singleTon.counter2);
}
}
//******************************************
counter1=1
counter1=0//*********************************************
class SingleTon{
public static int counter1;
public static int counter2=0;
private static SingleTon singleTon=new SingleTon();
public SingleTon(){
counter1++;
counter2++;
}
public static SingleTon getInstance(){
return singleTon;
}
}
public class MyTest {
public static void main(String[] args) {
SingleTon singleTon=SingleTon.getInstance();
System.out.println("counter1="+singleTon.counter1);
System.out.println("counter1="+singleTon.counter2);
}}
//******************************************************
counter1=1
counter1=1