123456789101112131415161718192021 |
- package com.sf.singleton;
- /**
- * 在使用时才创建对象
- * 懒汉式
- */
- public class Singleton2 {
- private static Singleton2 instance;
- private Singleton2(){}
- public static Singleton2 getInstance(){
- // 可以在第一次调用时创建对象
- // 如何区分是第一次 如果instance为null 再创建对象
- if(instance == null){
- instance = new Singleton2();
- }
- // 如果已经被创建过了 instance不为null 会返回上一次创建的对象
- return instance;
- }
- }
|