aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Clarify Question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Clarify Question" Watch "Clarify Question" New topic
Author

Clarify Question

d jones
Ranch Hand

Joined: Mar 13, 2006
Posts: 76
Just want to double-check this. I came across a Qn in the K&B Master Exam for SCJP 1.4

X x = getX(new X());//Line 1
....................
....................
.....................


On what line would the object created in line 1 be eligible for gc?

Is it correct to say that this question is ambiguous because there are 2 objects created on this line. x and the parameter to the getX method. Or am I missing out on some subtlety?

Many Thanks
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
Why do you think two objects are created on that line?
What is being passed back by that call to getX(new X()); ?

Please do not assume that all people capable of helping you have access to the K & B book (or want right now to look for it). I recommend that you present the complete problem you are trying to solve.


Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
d jones
Ranch Hand

Joined: Mar 13, 2006
Posts: 76
Hi,

The whole question isn't relevant in this case. My query concerns the first line only. I know that sometimes people like to know from where a question is sourced - that's only reason I mentioned K&B book. No extra material is required from the book to answer this question.

I think 2 objects are being created on this line:
1. new X() is creating an object which is a parameter in the getX method
2. The object which is returned from the getX method is being assigned to the reference variable x.

Therefore is it correct to say that there are 2 objects created here?

Many Thanks
Bert Bates
author
Sheriff

Joined: Oct 14, 2002
Posts: 8883
    
    5
Hi "d",

A couple of things:

1 - thanks for using our book, and thanks for indicating that your question comes from our book - that's the right thing to do, and all the authors of any of the materials you might want to ask about on the ranch will appreciate it if you mention the source - as you have!

2 - I agree that it would be best to list the entire question because if your understanding isn't quite complete, then it could be that another part of the question is important for the people who might try to help you out!

3 - Please update your display name!

Thanks, and good luck with your studies.

Bert


Spot false dilemmas now, ask me how!
(If you're not on the edge, you're taking up too much room.)
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 19064
    
  40

Originally posted by d jones:

I think 2 objects are being created on this line:
1. new X() is creating an object which is a parameter in the getX method
2. The object which is returned from the getX method is being assigned to the reference variable x.

Therefore is it correct to say that there are 2 objects created here?

Many Thanks


It really depends on what the getX() method does. Maybe it is returning the object that you passed to it. Maybe it is returning an X object that was passed in a previous method call. Maybe it is returning null. Without a clue on what the getX() method does, you don't know if it is creating another object or not.

So yes, the code for the getX() method is relevant to answer your question.

Henry
[ September 19, 2006: Message edited by: Henry Wong ]

Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
d jones
Ranch Hand

Joined: Mar 13, 2006
Posts: 76
Thank you all for your responses.

They are all fair points. Guess I was being just a little lazy.

The complete question is:

Given the following, and that class X looks like this:




at what point is the object created in line 13 eligible for garbage collection?

p.s. Great book Bert!!
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
According to my analysis, the instance of class X used as an actual parameter to method doStuff on line 13 is eligible for garbage collection before line 14 gets executed.
d jones
Ranch Hand

Joined: Mar 13, 2006
Posts: 76
Thanks Barry,

I agree with you about the parameter. But is there not also an object being returned from the doStuff method and being assigned to the reference variable x, which doesn't become eligible for gc until after line 15 has executed?

Why did you answer the question referring to the parameter object as opposed to the object assigned to the reference variable x?

Anyone else think this question is a bit unclear or am I not reading it correctly?

Thanks
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Clarify Question