• 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

Singleton pattern question in OCP Oracle Certified Professional Java SE 8 Programmer II (Sybex)

 
Ranch Hand
Posts: 49
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In the OCP Oracle Certified Professional Java SE 8 Programmer II, chapter 2 test answer to this question:

What are some of the properties of using the singleton pattern? (Choose all that apply.)


This answer is marked as being true:

F. Requires a public static method to retrieve the instance of the singleton.



You do not need a public static method to retrieve the instance.
This is also a valid singleton:
 
Marshal
Posts: 28226
95
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The Wikipedia article Singleton pattern agrees with you. It says

Wikipedia wrote:Typically, this is done by:

   declaring all constructors of the class to be private; and
   providing a static method that returns a reference to the instance.



And of course "typically" is not equivalent to a requirement, as you observe.
 
MyExamCloud Software Support
Posts: 160
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
An implementation of the singleton pattern must:

   ensure that only one instance of the singleton class ever exists; and
   provide global access to that instance.

So we must provide a static method that returns a reference to the instance.
 
Alexandru Dragoi
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I did provided a global access to my instance, although it does not respect the encapsulation principle (also another subject of the exam).
 
Bartender
Posts: 5465
212
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I agree. What is the difference between: Singleton.INSTANCE and Singleton.getInstance()?
 
author & internet detective
Posts: 41878
909
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
This is question #5 in the Sybex book. As taught in the book, the static method is used. (And I've always seen it that way on the exam too.)

The real exam tells you how many answers are valid though. So if you weren't sure whether to include it is an answer, that would be your clue.
 
Alexandru Dragoi
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Jeanne, good to know this!
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic