Singleton object- In static block or in getInstance(); which should be used
问题 Below are two ways to implement a singleton. What are the advantages and disadvantages of each? Static initialization: class Singleton { private Singleton instance; static { instance = new Singleton(); } public Singleton getInstance() { return instance; } } Lazy initialization is: class Singleton { private Singleton instance; public Singleton getInstance(){ if (instance == null) instance = new Singleton(); return instance; } } 回答1: Synchronized Accessor public class Singleton { private static