wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes I think D is right, but someone told me C is right. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "I think D is right, but someone told me C is right." Watch "I think D is right, but someone told me C is right." New topic
Author

I think D is right, but someone told me C is right.

arden liu
Greenhorn

Joined: Dec 09, 2004
Posts: 9
12. void start() {
13. A a = new A();
14. B b = new B();
15. a.s(b);
16. b = null;
17. a = null;
18. System.out.printIn(�start completed�);
19. }
When is the B object, created in line 14, eligible for garbage collection?
A. After line 16.
B. After line 17.
C. After line 18 (when the methods ends).
D. There is no way to be absolutely certain.
E. The object is NOT eligible for garbage collection.
=======================================================
I think you can save reference of b in s().
for example:
class myStaticClass{
static B saveb;
}
class A{
void s(B b){
myStaticClass.saveb = b;
}
...
}
I hope I am right, do you think so??
Thanks.
Nicholas Cheung
Ranch Hand

Joined: Nov 07, 2003
Posts: 4982
The instance you saved will be gone, if you explicitly set b to null.

Nick


SCJP 1.2, OCP 9i DBA, SCWCD 1.3, SCJP 1.4 (SAI), SCJD 1.4, SCWCD 1.4 (Beta), ICED (IBM 287, IBM 484, IBM 486), SCMAD 1.0 (Beta), SCBCD 1.3, ICSD (IBM 288), ICDBA (IBM 700, IBM 701), SCDJWS, ICSD (IBM 348), OCP 10g DBA (Beta), SCJP 5.0 (Beta), SCJA 1.0 (Beta), MCP(70-270), SCBCD 5.0 (Beta), SCJP 6.0, SCEA for JEE5 (in progress)
arden liu
Greenhorn

Joined: Dec 09, 2004
Posts: 9
Originally posted by Nicholas Cheung:
The instance you saved will be gone, if you explicitly set b to null.

Nick

Thanks anyway, But I do not agree with you.
b is one reference, but there is other reference can hold that object.
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
D is correct. When the method s of object a (that is of class A) is called it is passed a reference to the object that b refers to. We do not know what that method does with that reference. It could, for example, assign it to a static variable of the class A. Or it could create some Runnable object which stores the reference and start a thread which does who knows what. In any case, we do not know that zero references to the object that b refers to exist.

Yes, arden, your example would prevent the object that b refers to being eligible for garbage collection.
[ December 16, 2004: Message edited by: Barry Gaunt ]

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

Joined: Nov 07, 2003
Posts: 4982
Oh...sorry that I overlook your example. In case you use a static variable to hold it, the instance will not go away.

The question seems to have some ambigous because it does not specify what the method a.s(b) is. However, dont worried about that, the real questions will be more clear.

Nick
Shourya Kalinga
Greenhorn

Joined: Dec 14, 2004
Posts: 19
To support D Answer I was trying out some test scenario...may be this will be helpful.



Shourya
[ December 16, 2004: Message edited by: Shourya Kalinga ]

SCJP 1.4<br />SCWCD 1.4
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
"The question seems to have some ambigous because it does not specify what the method a.s(b) is".

That's the whole point of the question and exactly why choice D was given, and is the correct choice. Don't forget that SCJP 1.5 is going to be harder Nicholas
arden liu
Greenhorn

Joined: Dec 09, 2004
Posts: 9
Originally posted by Nicholas Cheung:
Oh...sorry that I overlook your example. In case you use a static variable to hold it, the instance will not go away.

The question seems to have some ambigous because it does not specify what the method a.s(b) is. However, dont worried about that, the real questions will be more clear.

Nick

Thanks Barry Gaunt and Nicholas Cheung.
I just try some questions from internet that are not clear. So Sometime, I was confused by the answer. I hope the real exam. could be 100% exact.
[ December 16, 2004: Message edited by: arden liu ]
Nicholas Cheung
Ranch Hand

Joined: Nov 07, 2003
Posts: 4982
Shourya,

Your example works because the variable xx is in fact in the same class of the XClass.

What Arden wanna point out is, if the variable xx is NOT of the same class, say, in YClass, this does not work!



Nick
Nicholas Cheung
Ranch Hand

Joined: Nov 07, 2003
Posts: 4982

Don't forget that SCJP 1.5 is going to be harder Nicholas

I wish the questions will not be too tricky.

By the way, Barry, will you go for SCJP Tiger?

Nick
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
"By the way, Barry, will you go for SCJP Tiger?"

Most likely. But not too seriously (in the sense of the quantitivity value of my score). I'm more interested in seeing the new form of the exam and helping to ensure that the exam is quality-wise OK. And, of course, so that I can continue helping out here.
Nicholas Cheung
Ranch Hand

Joined: Nov 07, 2003
Posts: 4982
In addition to MCQ and DND, will there be any new question type?

Nick
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
Originally posted by Nicholas Cheung:
In addition to MCQ and DND, will there be any new question type?

Nick


No idea.
 
 
subject: I think D is right, but someone told me C is right.
 
Similar Threads
Help! GC question #1.
A GC Question
Garbage Collection Confusion
Eligible for garbage collection