package com.wl.classloader;
class Singleton {
public static int counter1;
public static int counter2 = 0;
private static Singleton singleton = new Singleton();
private Singleton() {
counter1++;
counter2++;
}
public static Singleton getInstance(){
return singleton;
}
}
public class SingleTest{
public static void main(String[] args) {
Singleton ton = Singleton.getInstance();
System.out.println(ton.counter1);
System.out.println(ton.counter2);
}
}
结果打印为1 1
package com.wl.classloader;
class Singleton {
private static Singleton singleton = new Singleton();
public static int counter1;
public static int counter2 = 0;
private Singleton() {
counter1++;
counter2++;
}
public static Singleton getInstance(){
return singleton;
}
}
public class SingleTest{
public static void main(String[] args) {
Singleton ton = Singleton.getInstance();
System.out.println(ton.counter1);
System.out.println(ton.counter2);
}
}
结果打印为1 0