java静态变量声明和初始化

public  class  SingleTon {
  	private static SingleTon singleTon = new SingleTon();
  	public static int count1;
  	public static int count2 = 0;
  	private SingleTon() {
    		count1++;
    		count2++;
  	}
	public static SingleTon getInstance() {
    		return singleTon;
 	}

 	public static void main(String[] args) {
    		SingleTon singleTon = SingleTon.getInstance();
   	 	System.out.println("count1=" + singleTon.count1);
      	 	System.out.println("count2=" + singleTon.count2);
     	}
}
 
  

输出结果为:

count1=1

count2=0

debug查看执行顺序:

1.声明静态变量(赋默认值,此时count1和count2均为0)

2.执行构造器(对count1和count2进行++计算,此时count1和count2均为1)

3.对count2进行赋值(此时count1=1,count2为0)

你可能感兴趣的:(Java备忘回忆录)