• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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]
 
author & internet detective
Posts: 41860
908
Eclipse IDE VI Editor Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks a lot for the clarification on why F in included!
 
Ranch Hand
Posts: 76
3
IntelliJ IDE Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 41860
908
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
reply
    Bookmark Topic Watch Topic
  • New Topic