Java SE 8 Programmer II — Question 188
You want to create a singleton class by using the Singleton design pattern.
Which two statements enforce the singleton nature of the design? (Choose two.)
Answer options
- A. Make the class static.
- B. Make the constructor private.
- C. Override equals() and hashCode() methods of the java.lang.Object class.
- D. Use a public reference to point to the single instance.
- E. Implement the Serializable interface.
- F. Make the single instance created static and final.
Correct answer: B, F
Explanation
Making the constructor private (option B) prevents external instantiation, ensuring only one instance can be created within the class itself. Declaring the single instance as static and final (option F) ensures that it remains constant and is associated with the class rather than any object instance, maintaining the singleton property. The other options do not enforce the singleton nature effectively.