• 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:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Bear Bibeault
  • Knute Snortum
  • Liutauras Vilda
Sheriffs:
  • Tim Cooke
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Ron McLeod
  • Ganesh Patekar
  • salvin francis
Bartenders:
  • Tim Holloway
  • Carey Brown
  • Stephan van Hulst

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: 23968
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
Posts: 151
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).
 
Master Rancher
Posts: 3036
106
  • 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
Posts: 39054
714
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!
 
Attractive, successful people love this tiny ad:
RavenDB is an Open Source NoSQL Database that’s fully transactional (ACID) across your database
https://coderanch.com/t/704633/RavenDB-Open-Source-NoSQL-Database
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!