package top.qaqaq.java.P340;
public class SingletonTest {
public static void main(String[] args) {
Bank bank1 = Bank.instance;
// Bank.instance = null;
Bank bank2 = Bank.instance;
System.out.println(bank1 == bank2);
}
}
class Bank{
private Bank() {
}
public static final Bank instance = new Bank();
}
package top.qaqaq.java.P340;
/*
* 饿汉式单例模式写法二:使用了静态代码块
*
*/
public class SingletonTest1 {
public static void main(String[] args) {
}
}
class Order{
//1. 私有化类的构造器
private Order() {
}
//2. 声明当前类对象,没有初始化
//4. 此对象也必须声明为static的
private static Order instance = null;
static {
instance = new Order();
}
//3. 声明public、static的返回当前类对象的方法
public static Order getInstance() {
return instance;
}
}