class Singleton{
private static Singleton instance = new Singleton();
public static int a;
public static int b = 0;
private Singleton(){
super();
a++;
b++;
}
public static Singleton getInstance(){
return instance;
}
}
public class SingletonStatic {
public static void main(String[] args){
Singleton singleton = Singleton.getInstance();
System.out.println(singleton.a);
System.out.println(singleton.b);
System.out.println(singleton.getClass().getClassLoader());
}
}
输出结果:a=1,b=0;
class Singleton{
public static int a;
public static int b = 0;
private static Singleton instance = new Singleton();
private Singleton(){
super();
a++;
b++;
}
public static Singleton getInstance(){
return instance;
}
}
public class SingletonStatic {
public static void main(String[] args){
Singleton singleton = Singleton.getInstance();
System.out.println(singleton.a);
System.out.println(singleton.b);
System.out.println(singleton.getClass().getClassLoader());
}
}
输出结果:a=1,b=1