• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

SCJP creation of singleton question

 
Jer Mans
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4566
8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 41
Eclipse IDE Java Slackware
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic