wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes How long is a class loaded for? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "How long is a class loaded for?" Watch "How long is a class loaded for?" New topic
Author

How long is a class loaded for?

Richard Hayward
Ranch Hand

Joined: Feb 15, 2012
Posts: 68
    
    1
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

Joined: Dec 05, 2012
Posts: 305

When you run your program thru command >java StaticScopeTest , Classloader loads this class in memory, and it will be there till it's execution.


My one regret in life is that I am not someone else.
- Woody Allen
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41630
    
  55
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.


Ping & DNS - my free Android networking tools app
Richard Hayward
Ranch Hand

Joined: Feb 15, 2012
Posts: 68
    
    1
Thanks for the clarification guys.

Regards
Richard
 
 
subject: How long is a class loaded for?