About Question enthuware.ocpjp.v7.2.1187 :

Help and support on OCA OCP Java Programmer Certification Questions
1Z0-808, 1Z0-809, 1Z0-815, 1Z0-816, 1Z0-817

Moderator: admin

Post Reply
Ciprian Mihalache
Posts: 51
Joined: Wed Sep 28, 2011 12:14 pm
Contact:

About Question enthuware.ocpjp.v7.2.1187 :

Post by Ciprian Mihalache »

I agree it is a very stupid question and the answer is (very) debatable.
But, in case I get a similar question on the exam, I am seriously considering to choose "getInstance" instead of "newXXX".

Your examples for "newXXX" factory methods are perfectly valid. I also noticed a lot of other "newXXX" in several other places:
- java.nio package:
+ FileSystems.getDefault().newWatchService();
+ several methods in Files class
+ several methods in Channels class
+ some other methods located in various classes in this package
- java.util.concurrent package (your examples + some other methods that create threads, tasks, lock conditions etc.)
- other places

I also agree that we are used to have "getInstance" method in a Singleton class. But, in case the "getInstance" method receives arguments, I believe it acts as a Factory. I want to illustrate this with your "Singleton example" : Currency.getInstance(Locale). If you look over the implementation, I believe you agree that it is a Factory; it does not provide a Singleton reference.
Maybe the best example is Calendar.getInstance(Locale). In the implementation there are various Calendar types (GregorianCalendar, BuddhistCalendar, JapaneseImperialCalendar), but it is hidden to the client the actual class that is being instantiated.
And there are plenty of other classes with "getInstance" methods that have arguments.

In fact neither Calendar.getInstance() is not returning a Singleton. This can be very easily spotted from the implementation or from the following code (the result is 2 different numbers so there are 2 different instances):

Code: Select all

Calendar instance1 = Calendar.getInstance();
Thread.sleep(100);
Calendar instance2 = Calendar.getInstance();
System.out.println(instance1.hashCode());
System.out.println(instance2.hashCode());
So, for sure both "getInstance" and "newXXX" are very strong candidates for the right answer. For me the greatest argument for selecting "newXXX" would be a testimonial of someone that scored 100% and choosed this answer. If you want to stay with "newXXX" as the right answer, please change the explanation for "getInstance" option, in order to indicate some real Singleton implementations.

admin
Site Admin
Posts: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocpjp.v7.2.1187 :

Post by admin »

Hi Ciprian,
Your argument is very valid and we will add it to the explanation. As far as which answer is correct for the exam, it is anybody's guess.

thank you!
-Paul.
If you like our products and services, please help us by posting your review here.

rvdberg
Posts: 4
Joined: Sat May 30, 2015 4:53 am
Contact:

Re: About Question enthuware.ocpjp.v7.2.1187 :

Post by rvdberg »

As part of the SE II programmer exam:
RowsetFactory.createXXX()..

But okay, maybe createXXX() is the least used option..

danidark9312
Posts: 4
Joined: Tue Jul 10, 2018 5:28 pm
Contact:

Re: About Question enthuware.ocpjp.v7.2.1187 :

Post by danidark9312 »

How do you know "getInstance" is not using singleton pattern ? ,how would one guess in the examn which clases uses singleton or factory when they call getInstance, because getInstance is part of the standar naming for singleton mehotd.

Post Reply

Who is online

Users browsing this forum: No registered users and 24 guests