aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Garbage Collection ... Need Clarification Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Garbage Collection ... Need Clarification" Watch "Garbage Collection ... Need Clarification" New topic
Author

Garbage Collection ... Need Clarification

Sumeet Chakraborty
Ranch Hand

Joined: Feb 05, 2009
Posts: 45
Hi all. This question is from K & B Book (Chap -3 , Self Test Q No 11).

I need the clarification for the answer given , because its bit confusing for me.



When "do stuff" is reached , how many objects will be eligible for Garbage Collection ?

The answer is 1 (according to the book and obviously its right )
The answer Explanation is : It should be clear that there is still a reference to the object reffered by a2, and that there is still a reference to to the object referred to by a2.b2. (Its fine up till here)
What might be less clear is that you can still access the other Beta object through the static variable a2.b1 -- because its static. (This is where I am confused)


Te above code never assigns anything to a2.b1. But still how can it refer the Beta Object ???

It may be something very simple logic which I am missing ...for sure. But somehow I am getting confused.
Please help ...


avi sinha
Ranch Hand

Joined: Mar 15, 2009
Posts: 453

Sumeet Chakraborty wrote:
Te above code never assigns anything to a2.b1. But still how can it refer the Beta Object ???

what is bets here in line 17??? if it is a class where is the definition???

if it is beta then the explanation may be:

the point is that static members are the properties of the class not of any particular object.that's why there is only one copy of a static member and
that would last until atleast one object of the class is on the heap and has a valid refernce.

here for clarification just consider each object of class alpha has a static member Beta b1.

but when you write a1.b1=b1 both a1.b1 & a2.b1 now refers to b1.

that's why when a1 is set to null a2.b1 is referring to b1. & object b1 of class Beta is not eligible for gc.

avi


SCJP 5.0 SCWCD 5.0
avi sinha
Ranch Hand

Joined: Mar 15, 2009
Posts: 453

clearly here object 1 topmost here in the heap diagram can be accessed by a2.b1
object 2 2nd from top here in the heap diagram can be accessed by a2.b2
object 3 below object 2 here in the heap diagram can't be accessed
object 4 at the bottom here in the heap diagram can be accessed by a2


avi



[Thumbnail for Noname.jpg]

Sumeet Chakraborty
Ranch Hand

Joined: Feb 05, 2009
Posts: 45
Yes .. That is Beta in line 17 and not Bets Sorry for the mistake, I have rectified it .

And thanks Avi for the clarification. Its quite clear for me now. Static variables and Garbage collection are the 2 things which are confusing me a lot
Bob Wheeler
Ranch Hand

Joined: Apr 24, 2009
Posts: 317
Sumeet Chakraborty wrote:

the point is that static members are the properties of the class not of any particular object.that's why there is only one copy of a static member and
that would last until atleast one object of the class is on the heap and has a valid refernce.

here for clarification just consider each object of class alpha has a static member Beta b1.

but when you write a1.b1=b1 both a1.b1 & a2.b1 now refers to b1.

that's why when a1 is set to null a2.b1 is referring to b1. & object b1 of class Beta is not eligible for gc.

avi


Thanks Avi, I was jst checking this Q.

rob


SCJP 6 - SCJD - SCWCD 5 - SCBCD 5
JavaEnterpriseEditionFaq - TomcatFaq
Pete Delrue
Greenhorn

Joined: Apr 28, 2009
Posts: 2
Does it really say in the book on line 21: a1.b1=b1?

That doesnt make any sense,according to me it should say Alpha.b1=b1.
There is no a1.b1 or a2.b1, isnt that the point of the keyword static?
avi sinha
Ranch Hand

Joined: Mar 15, 2009
Posts: 453

Pete Delrue wrote:
That doesnt make any sense,according to me it should say Alpha.b1=b1.


you can access the static member b1 by alpha.b1 but it can also be accessed by an instance of class alpha
as
alpha a1=new alpha();
a1.b1;
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Garbage Collection ... Need Clarification