1Z0-809 · Question #211
Which two are elements of a singleton class? (Choose two.)
The correct answer is B. a public method to instantiate the single instance D. a private constructor to the class. A singleton class requires a private constructor (D) to prevent external code from creating new instances directly, and a public method (B) - typically called getInstance() - that controls access to the one and only instance, creating it on first call and returning the existing…
Question
Options
- Aa transient reference to point to the single instance
- Ba public method to instantiate the single instance
- Ca public static method to return a copy of the singleton reference
- Da private constructor to the class
- Ea public reference to point to the single instance
How the community answered
(22 responses)- A9% (2)
- B73% (16)
- C5% (1)
- E14% (3)
Explanation
A singleton class requires a private constructor (D) to prevent external code from creating new instances directly, and a public method (B) - typically called getInstance() - that controls access to the one and only instance, creating it on first call and returning the existing one thereafter.
Why the distractors are wrong:
- A -
transientis a Java keyword that excludes a field from serialization; it has no role in defining a singleton. - C - The
getInstance()method returns the actual singleton reference, not a copy. Returning a copy would defeat the purpose of enforcing a single instance. - E - The internal static reference must be private, not public. Exposing it publicly would let callers reassign or misuse it, breaking the singleton guarantee.
Memory tip: Think "lock the door, provide the key" - the private constructor locks the door so no one can new their way in, and the public method is the only key that controls entry to the single instance.
Community Discussion
No community discussion yet for this question.