Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How long is a class loaded for?

 
Richard Hayward
Ranch Hand
Posts: 101
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Suppose this code:



Output:
>java StaticScopeTest
0
123


Is there an absolute guarantee that the jvm will not need so much ram during the execution of whatever complicated stuff happens at line 10, it decides to unload MyClass, and consequently, when line 11 is reached, MyClass.x is back to zero?

Regards
Richard
 
meeta gaur
Ranch Hand
Posts: 305
Notepad Tomcat Server Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When you run your program thru command >java StaticScopeTest , Classloader loads this class in memory, and it will be there till it's execution.
 
Ulf Dittmer
Rancher
Pie
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The only way for a class to get unloaded (i.e., GCed) is if its classloader gets unloaded. In this particular case -where the only classloaders involved are the ones the JVM creates for running the app- there is no possibility of the class getting unloaded before the JVM exits.

In general, you never need to worry about classes getting unloaded unless you're using code whose very point is to do just that - like OSGi environments. For the purposes of the exam, there's nothing to worry about.
 
Richard Hayward
Ranch Hand
Posts: 101
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the clarification guys.

Regards
Richard
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic