1. 程式人生 > >建立類例項的第六種方式

建立類例項的第六種方式

今天在閱讀springboot原始碼時,發現建立物件不同於以往new關鍵字Constructor建立clone反射呼叫以及反序列化的第六種寫法。

宣告

public class ApplicationConversionService extends FormattingConversionService {

	private static volatile ApplicationConversionService sharedInstance;

通過點語法建立例項

public static ConversionService getSharedInstance() {
		ApplicationConversionService sharedInstance = ApplicationConversionService.sharedInstance;
		if (sharedInstance == null) {
			synchronized (ApplicationConversionService.class) {
				sharedInstance = ApplicationConversionService.sharedInstance;
				if (sharedInstance == null) {
					sharedInstance = new ApplicationConversionService();
					ApplicationConversionService.sharedInstance = sharedInstance;
				}
			}
		}
		return sharedInstance;
	}