1、私有化类的构造器
2、创建类的对象
3、提供公共的静态方法,返回类的对象
4、要求此对象也必须声明为静态的
public class bank {private bank() {}private static bank instance = new bank();private static bank getInstance() {return instance;}public static void main(String[] args) {bank.getInstance();}}
下面是测试代码
public class Bank {private Bank() {}private static Bank instance = new Bank();private static Bank getInstance() {return instance;}public static void main(String[] args) {Bank bank1 = Bank.getInstance();Bank bank2 = Bank.getInstance();System.out.println(bank1 == bank2);}}