• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Clarify Question

 
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
d jones
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
author
Posts: 9050
21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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 ]
 
d jones
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
reply
    Bookmark Topic Watch Topic
  • New Topic