• Post Reply Bookmark Topic Watch Topic
  • New Topic

Adds the specified class to a Vector to keep it from being  RSS feed

 
Nikos Stavros
Ranch Hand
Posts: 243
Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
can someone explain in more detail how this works or point me to resources

 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16045
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I guess this is done because of the following: If there are no instances anymore of a class, the JVM could unload the class. Static members in the class would then also be garbage collected. If the application saves state in such variables, that state would be lost. If the application would later create a new instance of the class, the class would be loaded again and the static members would be initialised to their starting values instead of the saved state.

By keeping a reference to the class in a vector, you force the JVM to not unload it (it can't unload the class if the application still has a reference to it).

I found also this page about unloading classes: Class unloading - Clarifications and Amendments to the JLS - This was a change made in Java 1.2.

More info: Find out how Java 1.2 changes the way we load and unload classes in Java

Probably you don't need this trick with keeping a reference in a vector if you're using Java 1.2 or newer.
 
Nikos Stavros
Ranch Hand
Posts: 243
Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jesper, thanks I understand it better now
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!