视频教学中的单例模式,请教一下,为什么执行后,先打印出来“构造方法调用了”,然后才是"get方法调用了"。
public class SingletonTest {
public static void main(String[] args) {
Singleton singleton = Singleton.getInstance();
}
}
class Singleton
{
private static Singleton singleton = new Singleton();
private Singleton()
{
System.out.println("构造方法调用了");
}
public static Singleton getInstance()
{
System.out.println("get方法调用了");
return singleton;
}
}
public class SingletonTest {
public static void main(String[] args) {
Singleton singleton = Singleton.getInstance();
}
}
class Singleton
{
private static Singleton singleton = new Singleton();
private Singleton()
{
System.out.println("构造方法调用了");
}
public static Singleton getInstance()
{
System.out.println("get方法调用了");
return singleton;
}
}