Java SE 8 Programmer II — Question 39
You want to create a singleton class by using the Singleton design pattern.
Which two statements enforce the singleton nature of the design?
Answer options
- A. Make the class private.
- B. Make the constructor equals() and hashCode() methods of the java.lang.Object class.
- C. Override static reference to point to the single instance.
- D. Use a Serializable interface.
- E. Implement the
Correct answer: A, B
Explanation
Option A is correct because making the class private prevents instantiation from outside the class. Option B is also correct as overriding equals() and hashCode() ensures that the singleton instance maintains its identity in collections. Options C, D, and E do not directly contribute to enforcing the singleton nature of the class.