Hi,
Originally posted by Kitty Dayal:
My only doubt is: Isn't the b (argument to m1() ) created each time the m1() is invoked and hence the object B("Ba") will still have b reference and so cannot be gc'ed.
(Totally).
I don't know if I understood you question, but
I'll give it a shot.
The parameter b is just used to pass the
reference determined by the argument:
the newly created B object. The parameter
is not maintaining this reference as a
member variable would do, in a way it
forgets what happened as soon as the
method is left.
The static variable bc of type B, in the case
of this exercise, first holds a reference to
Ba. This reference is lost as soon as m1() is
called a second time, because the value is
replaced by the reference to Bb. Therefore
Ba is eligible for GC since there are no
more references to it.
If the variable bc wasn't static and you'd
create an instance of class J before every
call to m1() then Ba would still have it's
reference and would not be eligible for GC.
Cheers,
Gian Franco Casula
[ August 16, 2004: Message edited by: Gian Franco Casula ]