Win a copy of Learning OpenStack Networking: Build a solid foundation in virtual networking technologies for OpenStack-based clouds this week in the Cloud/Virtualization forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Knute Snortum
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Ganesh Patekar
  • Stephan van Hulst
  • Pete Letkeman
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Ron McLeod
  • Vijitha Kumara

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

 
Ranch Hand
Posts: 49
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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:
 
Sheriff
Posts: 23706
50
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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
Ranch Hand
Posts: 142
1
  • Mark post as helpful
  • send pies
  • 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
  • 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).
 
Rancher
Posts: 2829
96
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I agree. What is the difference between: Singleton.INSTANCE and Singleton.getInstance()?
 
author & internet detective
Sheriff
Posts: 38564
654
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
Thanks Jeanne, good to know this!
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!