This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Garbage Collection Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Garbage Collection" Watch "Garbage Collection" New topic
Author

Garbage Collection

Max Campa
Greenhorn

Joined: Oct 31, 2009
Posts: 10
Hi there,

can anyone explain to me the following:

Given:


When line 20 is reached, how many objects will be eligble for garbage collection?

The correct answer is 1 and the explanation is:

It should be clear that there is still a reference to the object referred to by a2 ( I understand that), and that there is still a reference to the object referred to by a2.b2 ( I understand that).

What might be less clear is that you can still access the other Beta object trough the static variable a2.b1 becausae it s static ( I dont understand this).

Which object is elegible for GC?

Regards

Max
Deepak Bala
Bartender

Joined: Feb 24, 2006
Posts: 6661
    
    5

It is hard to read code that is not inside the CODE tags. Can you please edit your post and put your code inside these tags ?


SCJP 6 articles - SCJP 5/6 mock exams - More SCJP Mocks
George Coolidge
Greenhorn

Joined: Apr 08, 2009
Posts: 11
The reason is even though

looks like that an instance variable is assigned, it is actually assigning a class variable, aka, static variable. So b1 is still referenced in Alpha.b1, remember, even though a2 gets collected, b1 still won't be collected since you don't need to instantiate an instance to assign a class variable.

So only a1 is not referenced by anybody and becomes dangling reference, and therefore only 1 object will be garbage collected. In real code, the form in

is discouraged since it is confusing to the readers and sometimes coders themselves.
Max Campa
Greenhorn

Joined: Oct 31, 2009
Posts: 10
Thank you George. I have debugged the code and now it makes sense.
George Coolidge
Greenhorn

Joined: Apr 08, 2009
Posts: 11
You are always welcome.
Max Campa wrote:Thank you George. I have debugged the code and now it makes sense.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Garbage Collection
 
Similar Threads
What does this assignment mean?
Double K&B Chapter 3- Question 11
Garbage Collection Clarification
Garbage Collection problem-2
Island of isolation