answer F claims "Requires a public static method to retrieve the instance of the singleton."
In my view, method can have static (default/package level) method access, so the singleton is for other classes within the same package, and it's too a proper singleton pattern since other classes can retrieve the instance of the singleton.
Many thanks in advance for your thoughts!
Jeanne Boyarsky wrote:In the real world, you are correct. In the simplified world of the exam, the singleton is shared more broadly. On the bright side, the real exam tells you how many answers are correct. If it said "choose three", I bet you'd have included F!
No. I think this would be an unacceptable mistake in the question if it happened on the real exam. A singleton simply does not require the instance to be publicly available. Moreover, a method isn't the only way to expose data.
Now, I realise that using a method to return the instance follows encapsulation and all that, but in the case of a singleton I do not think it adds any value.
In my opinion, the best way to expose a singleton instance is with a (public) static final field initialised at declaration.
So that's at least one alternative. I know the exam likes to be vague and tricky, but here I think it is just wrong.
Ole Sandum wrote:No. I think this would be an unacceptable mistake in the question if it happened on the real exam.
And you are entitled to your opinion. However, the exam doesn't include everything there is in Java. It includes common idioms for singleton. Reading into a question invites the possibility of getting it wrong.
Ole Sandum wrote: A singleton simply does not require the instance to be publicly available. Moreover, a method isn't the only way to expose data.
I agree with you. However, a singleton exposed by a package private variable isn't the most common way of doing it.