java static笔试题

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;
    }
}
	@Test
    public void test() {
     
        SingleTon singleTon = SingleTon.getInstance();
        System.out.println("count1=" + singleTon.count1);
        System.out.println("count2=" + singleTon.count2);
    }

问 count1=?,count2=?
count1=1,count2=0,static变量在类加载时按照自上而下的顺序执行,在new SingleTon()count2又重新赋值成0

你可能感兴趣的:(java,base)