Which two are elements of a singleton class? (Choose two.)
Which two are elements of a singleton class? (Choose two.)
A singleton class typically includes a private constructor to prevent direct instantiation (so that only one instance of the class can be created) and a public static method that returns the sole instance of the class. The private constructor ensures that no other instances can be created except through the class itself, and the public static method provides a global point of access to the instance.
C,D, not B otherwise could keep calling that public method and instantiate multiple times
public class SingletonGreed { private static SingletonGreed instance = new SingletonGreed(); private SingletonGreed(){} public static SingletonGreed getInstance () { return instance; } }
C,D checked.
Considering the standard Singleton implementation (i.e. the Bill Pugh implementation) : public class BillPughSingleton { private BillPughSingleton(){} private static class SingletonHelper{ private static final BillPughSingleton INSTANCE = new BillPughSingleton(); } public static BillPughSingleton getInstance(){ return SingletonHelper.INSTANCE; } } We can conclude that answer is CD
anser is C D
C and D are correct since we need static method to return instance of singleton and private constructor so that it is not possible to create multiple instances.
Answer : CD
it is BD because constructor has to be private to prevent instantiation and also there must be a method to check if there is already an object of it to provide otherwise create a new one.
D E~~~!!!