How to write a Singleton in proper manner?

后端 未结 13 1131
走了就别回头了
走了就别回头了 2020-12-23 12:03

Today in my interview one interviewer asked me to write a Singleton class. And i gave my answer as

public class Singleton {

    private static Singleton re         


        
13条回答
  •  抹茶落季
    2020-12-23 12:49

    From What is an efficient way to implement a singleton pattern in Java?

    Use an enum:

     public enum Foo 
     {
       INSTANCE;
     }
    

    Joshua Bloch explained this approach in his book 'Effective Java'

    Also check out The better Java singleton pattern nowadays?

提交回复
热议问题