Which two properly implement a Singleton pattern?

Which two properly implement a Singleton pattern?

Which two properly implement a Singleton pattern?

A.
class Singleton { private static Singleton instance; private Singleton () {} public static
synchronized Singleton getInstance() { if (instance == null) { instance = new Singleton (); }
return instance; } }

B.
class Singleton { private static Singleton instance = new Singleton(); protected Singleton
() {} public static Singleton getInstance () { return instance; } }

C.
class Singleton { Singleton () {} private static class SingletonHolder { private static final
Singleton INSTANCE = new Singleton (); } public static Singleton getInstance () { return
SingletonHolder.INSTANCE; } }

D.
enum Singleton { INSTANCE; }



Leave a Reply 1

Your email address will not be published. Required fields are marked *