码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2016-08-04 01:32:24      阅读:134      评论:0      收藏:0      [点我收藏+]

标签:

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程序员第一次看到都会犯的错,是不是很牛逼的样子。。

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

 答案下回详细分解。。?? 

 

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

标签:

原文地址:http://www.cnblogs.com/summercoder/p/5735111.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!