给大家看段比较有意思的代码

package code.classloader;

public class Interesting {

    public static int count1;
    public static int count2 = 0;
    public static Interesting ins = new Interesting();
    
    private Interesting() {
        count1++;
        count2++;
    }
    
    public static Interesting getInstance() {
        return ins;
    }
    
    public static void main(String[] args) {
        Interesting ins = Interesting.getInstance();
        System.out.println(ins.count1);
        System.out.println(ins.count2);
    }
    
}

看看这段代码,想一下输出结果。。。count1=? count2=?

然后再看另外一段

 

package code.classloader;

public class Interesting {

	public static Interesting ins = new Interesting();
	public static int count1;
	public static int count2 = 0;
	
	private Interesting() {
		count1++;
		count2++;
	}
	
	public static Interesting getInstance() {
		return ins;
	}
	
	public static void main(String[] args) {
		Interesting ins = Interesting.getInstance();
		System.out.println(ins.count1);
		System.out.println(ins.count2);
	}
	
}

 有没有看到区别啊??看不出来的先去挂个眼科哈!!!想想这段代码的输出结果 count1 和count2的值

 这个问题号称全世界百分之九十的java程序员第一次看到都会犯的错,是不是很牛逼的样子。。

 这段代码虽然很简单,但却是涵盖了类加载的一系列过程。

 答案下回详细分解。。😁 

 

posted @ 2016-08-04 00:18  summer_coder  阅读(4040)  评论(0编辑  收藏  举报