File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Another Garbage collection 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 "Another Garbage collection" Watch "Another Garbage collection" New topic

Another Garbage collection

B pandoo
Ranch Hand

Joined: Nov 14, 2006
Posts: 37
Assume my class is a class that has 2 methods f() and g().How many objects will be eligible for garbage collection by the time you invoke g() ?
Myclass c1 =new Myclass();
Myclass c2 =new Myclass();
c1 = null;
c2 =c1;


1) 0
2) 1
3) 2
4)The given information is insufficient.

My opinion is 2 objects are eligible for garbage collection.Am i right or wrong?please correct me.
Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 10032

You are right, 2 objects are eligible for gc.

SCJP 1.4, SCWCD 1.4 - Hints for you, Certified Scrum Master
Did a rm -R / to find out that I lost my entire Linux installation!
Bert Bates

Joined: Oct 14, 2002
Posts: 8898
Does f() send or keep a copy of the reference somewhere?
[ November 21, 2006: Message edited by: Bert Bates ]

Spot false dilemmas now, ask me how!
(If you're not on the edge, you're taking up too much room.)
fred rosenberger
lowercase baba

Joined: Oct 02, 2003
Posts: 11955

i've always been a little shaky on GC, but i'd think 4 would be the right answer. mostly because you don't know what f() is doing with the reference. it is possible that object gets a new reference in that method, and would then therefore not be eligible.

I'm willing to listen to someone tell me why i'm wrong, though.

There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 10032


You are right. The info given from the wuestion is not enough.
I agree. Here's the link:
subject: Another Garbage collection
It's not a secret anymore!