java400-l-bounces@xxxxxxxxxxxx wrote on 10/20/2006 10:13:45 AM:

I have a question regarding the life of static variables within the VM.
I always thought that if you instantiated an object that contained
static variables, garbage collection would cleanup the object along with
the static variables.  For instance, say I have classes Pet and Cat.
Cat contains several static variables (say names of cats) and I
instantiate an instance of Cat from Pet.  Do the static variables within
Cat ever get GC'd.  This is something I obviously missed a long time ago
and feel a little sheepish for not knowing.

Static fields are stored in their class object (instance of
and classes are only GC'd when their loading classloader is GC'd.

So, if Cat is loaded by the same classloader as Pet, then the class Cat
survive as long as the class Pet does.

A tangential question: why static fields?  A real "Cat" object would have
the 'name' as an instance field, eh?  Just a thought...


As an Amazon Associate we earn from qualifying purchases.

This thread ...


Follow On AppleNews
Return to Archive home page | Return to MIDRANGE.COM home page

This mailing list archive is Copyright 1997-2022 by and David Gibbs as a compilation work. Use of the archive is restricted to research of a business or technical nature. Any other uses are prohibited. Full details are available on our policy page. If you have questions about this, please contact [javascript protected email address].

Operating expenses for this site are earned using the Amazon Associate program and Google Adsense.