现在看一个问题:对象的创建方式有哪几种?
四种:new 、克隆、序列化、反射。
以下基于jdk1.8
单例模式(懒汉式,双重锁):
package com.company;
import java.io.Serializable;
/**
* Title:
*
* @Author
* @CreateTime 2019/6/3 18:49
*/
public class TestSingleton implements Serializable {
String name = null;
private TestSingleton() {
}
private static volatile TestSingleton instance = null;
public static TestSingleton getInstance() {
if (instance == null) {
synchronized (TestSingleton.class) {
if (instance == null) {
instance = new TestSingleton();
}
}
}