单例模式-饿汉式

package com.example.SpringBootTest1.shejimoshi.singleton;
public class HungrySingleton {
private static final HungrySingleton hungrySingleton = new HungrySingleton();
private HungrySingleton() {}

public static HungrySingleton getInstance() {
return hungrySingleton;
}
}

public class Test {
public static void main(String[] args) {
HungrySingleton instance1 = HungrySingleton.getInstance();
HungrySingleton instance2 = HungrySingleton.getInstance();
System.out.println(instance1 == instance2); // true
}
}

posted @ 2022-06-04 06:32  剑阁丶神灯  阅读(37)  评论(0)    收藏  举报