This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes SCJP creation of singleton question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "SCJP creation of singleton question" Watch "SCJP creation of singleton question" New topic
Author

SCJP creation of singleton question

Jer Mans
Greenhorn

Joined: Jun 11, 2011
Posts: 2
Hello all

I have a question about the following question in the book OCP Java SE 6 Programmer Practice Exams.





A. Compilation fails.

B. Class BeSafe can create many instances of SafeDeposit.

C. Class BeSafe CANNOT create any instances of SafeDeposit.

D. Class BeSafe can create only one instance of SafeDeposit.

E. Class BeSafe can create instances of SafeDeposit without using the getInstance() method.

F. Once class BeSafe has created an instance of SafeDeposit, it cannot change the valueof the instance’s "code" variable.

The correct answers are B and F. F is correct because the method getInstance is not synchronized (according to the book).

In my opinion Besafe can not make a new instance of SafeDeposit, because the
constructor is private. It can only "ask" for an instance. The only class that can create instances of
SafeDeposit is SafeDeposit himself.

Is my interpretation wrong? Or is this an error in the book?
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4344
    
    8

Hi Jer. Welcome to The Ranch!

I think you're trying to make a distinction that isn't really warranted, there. My interpretation would be that calling getInstance can cause an instance to be created, so I wouldn't have any particular problem saying that the caller has created an instance. And I think the wording of option E backs up that interpretation.

I don't think you could call it an error in the book. The worst you could say is that the book has phrased the question in a way you find ambiguous - but it's clear from their answer what they consider the correct interpretation to be.

Just my opinion, anyway.
Jer Mans
Greenhorn

Joined: Jun 11, 2011
Posts: 2
Thanks for your answer. The only thing I'm wondering about.
Will there be questions like this on the real exam, and if so how should it be interpreted on the real exam?
Montgomery Buslingthorpe
Greenhorn

Joined: Jul 24, 2012
Posts: 4
B. Class BeSafe can create many instances of SafeDeposit. <---- I don't think so.....

Yes, it's not possible to create an instance of SafeDeposit from outside of SafeDeposit since its constructor is private, but calling SafeDeposit.getInstance(123); will result in ONE instance, and this ONE instance will be returned to any further calls to SafeDeposit.getInstance(1);

Seeing that SafeDeposit does not provide any setter for private int code and the only way to set private int code is through the constructor, private int code cannot be changed.

So on the one hand you have the book saying that many instances can be created, and then on the other hand, saying that private int code cannot be changed (Which is the result of only being able to have one instance, not many)
Yalvin Duha
Ranch Hand

Joined: Apr 07, 2012
Posts: 40

Pi Xel wrote:B. Class BeSafe can create many instances of SafeDeposit. <---- I don't think so.....

So on the one hand you have the book saying that many instances can be created, and then on the other hand, saying that private int code cannot be changed (Which is the result of only being able to have one instance, not many)

You have to see the problem in the context of multi-threaded environment.

If you have multiple threads entering getInstance() method, and all lose their turn while between line "4" and "5", then once each of them become runnable, since they have by passed the conditional check (null), they will create new SafeDeposit instances. It's just that, since there won't be any reference to these instances, they will be lost and only the last one will remain.

So, in a sense, it does create many instances but under a very loosely definition which can be vague.

Finally, the option "F" states explicitly when the instance of SafeDeposit has been created, that is when the getInstance() method has returned, then the instance variable "code" cannot be changed which is true.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: SCJP creation of singleton question
 
Similar Threads
Question regaring Singleton.
Static Variables and Methods
Singleton Pattern
Meaning of private constructors, final methods
what is the use of this " static A a = new A() "