views:

83

answers:

1

The answer to 'unloading classes in java' says -

"The only way that a Class can be unloaded is if the Classloader used is garbage collected." I took a look at the JLS but couldn't understand it

Why is this the case?

+8  A: 

A class is only unloaded when it is garbage collected, and for that to happen there must be no references to it anywhere. And the classloader keeps a reference to each class it loads.

invariant
Thanks. Couldn't be clearer than that (+:
Everyone
And why does it keep a reference?
Mnementh
@Mnementh - Is it for reinstantiation if so desired?
Everyone
It keeps a ref to it so if it is asked to load it again it can just return the same one. Otherwise you would have multiple instances of the same class object being loaded with different refs to them floating around.
invariant