This week's book giveaway is in the Agile and other Processes forum.
We're giving away four copies of The Mikado Method and have Ola Ellnestam and Daniel Brolund 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
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Reply 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: 6590
    
    1

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 - SCJP Mocks - SCJP 5 Mock exam (Word document ) - SCJP 5 Mock exam in Java.Inquisition format
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://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: Garbage Collection
 
Similar Threads
Island of isolation
What does this assignment mean?
Garbage Collection problem-2
Garbage Collection Clarification
Double K&B Chapter 3- Question 11