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程序员第一次看到都会犯的错,是不是很牛逼的样子。。
这段代码虽然很简单,但却是涵盖了类加载的一系列过程。
答案下回详细分解。。??
时间: 2024-10-06 08:21:05