题主初步接触单例模式,在写了一个懒汉加载的单例模式demo之后,发现主方法运行多次时,创建的单例hash值一样,为什么重新启动程序单例的hash值还是一样呢
public class Singleton {
private static Singleton singleton = new Singleton();
public static Singleton getInstance(){
return singleton;
}
public static void main(String[] args) {
System.out.println(getInstance());
System.out.println(LocalDateTime.now());
}
}
Singleton@74a14482
2022-06-15T23:18:22.014
Process finished with exit code 0
Singleton@74a14482
2022-06-15T23:18:46.888
Process finished with exit code 0