• Post Reply Bookmark Topic Watch Topic
  • New Topic

OCP 8 Chapter 2 Exam: Does Silngleton requires to have public static method to get the instance??  RSS feed

 
Artem Lovan
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In book "Java OCP 8 Programmer II Study Guide", Chapter 2 exam, question 5,
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! 
Screen-Shot-2016-10-22-at-4.42.16-PM.png
[Thumbnail for Screen-Shot-2016-10-22-at-4.42.16-PM.png]
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 36651
475
Eclipse IDE Java VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
Artem Lovan
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot for the clarification on why F in included!
 
Ole Sandum
Ranch Hand
Posts: 76
3
IntelliJ IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 36651
475
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!