据说是每个菜鸟都无法给出输出结果的,特无耻的面试题
据说是每个初学者都无法给出输出结果的,特无耻的面试题
请给出下面程序的输出结果:
public class ClassLoaderSeq {
/**
* @param args
*/
public static void main(String[] args) {
Singleton singleton = Singleton.getInstance();
System.out.println("counter1 = " + singleton.counter1);
System.out.println("counter2 = " + singleton.counter2);
}
}
public class Singleton {
private static Singleton singleton = new Singleton();
public static int counter1;
public static int counter2 = 0;
private Singleton(){
this.counter1++;
this.counter2++;
}
public static Singleton getInstance(){
return singleton;
}
}
这个让人纠结的输出结果:
counter1 = 1
counter2 = 0
请给出下面程序的输出结果:
public class ClassLoaderSeq {
/**
* @param args
*/
public static void main(String[] args) {
Singleton singleton = Singleton.getInstance();
System.out.println("counter1 = " + singleton.counter1);
System.out.println("counter2 = " + singleton.counter2);
}
}
public class Singleton {
private static Singleton singleton = new Singleton();
public static int counter1;
public static int counter2 = 0;
private Singleton(){
this.counter1++;
this.counter2++;
}
public static Singleton getInstance(){
return singleton;
}
}
这个让人纠结的输出结果:
counter1 = 1
counter2 = 0