单例模式-饿汉式
作者:互联网
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
}
}
标签:饿汉,getInstance,模式,static,private,单例,hungrySingleton,public,HungrySingleton 来源: https://www.cnblogs.com/ladeng19/p/15822889.html