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 how many objects are eligible for 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)
Reply locked New topic

how many objects are eligible for Garbage collection

Premak Rajan

Joined: Feb 12, 2007
Posts: 18
I have doubt in this program

1. public class BettyAck{
2. public static void main(String argv[]){
3. BettyAck b =new BettyAck();
4. }
5. public BettyAck() {
6. Integer x = new Integer(10);
7. findOut(x);
8. Integer y = new Integer(99);
9. Integer z = y;
10. z = null;
11. findOut(y);
12. //here
13. }
14. public void findOut(Integer y){
15. y = null;
17. }

my answer for this program is 3 objects are eligible for garbage collection.Is it correct or not?
Please anyone help me

with regards
Deepak Bala

Joined: Feb 24, 2006
Posts: 6662

No object is eligible for GC.

This is the situation

a -> b -> object

When you make reference a point to null

a-> null
b -> object

So you can still get to the object with reference b

SCJP 6 articles - SCJP 5/6 mock exams - More SCJP Mocks
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
This problem was recently discussed here.

Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
I agree. Here's the link:
subject: how many objects are eligible for Garbage collection
It's not a secret anymore!