File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Servlets and the fly likes class variable life cycle Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "class variable life cycle" Watch "class variable life cycle" New topic
Author

class variable life cycle

FY Hsieh
Ranch Hand

Joined: Aug 07, 2006
Posts: 73
For a static class variable (the class is bundled in the EAR deployed to app server), what's its life cycle ? Is it instantiated when the server starts and stays alive as long as the server is on ? Of course, when server crashes or is turned off, this variable is gone.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61191
    
  66

It just follows standard Java rules. When the class is loaded, the variable comes into being.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
FY Hsieh
Ranch Hand

Joined: Aug 07, 2006
Posts: 73
Originally posted by Bear Bibeault:
It just follows standard Java rules. When the class is loaded, the variable comes into being.


How long does the class survive ? do they survive from the moment server loads them to the moment server is down ?
Prabhu Venkatachalam
Ranch Hand

Joined: Nov 16, 2005
Posts: 502

JVM Spec says

A class or interface may be unloaded if and only if its class loader is unreachable.


So, Class loading and unloading is not related with server start up and shutdown. it is related with ClassLoader.

Each web application deployed in server will have it's own classloader. Class Loader will be unavaliable only when web application is undeployed. So, static Class variable stays till web application is in service.

hth,


Prabhu Venkatachalam<br />SCJP 1.4,SCWCD 1.4<br />prabhu.venkatachalam@gmail.com
Rahul Bhattacharjee
Ranch Hand

Joined: Nov 29, 2005
Posts: 2308
Originally posted by FY Hsieh:
Is it instantiated when the server starts and stays alive as long as the server is on ?


It is instantiated with the first reference to it , nothing to do with the applications startup.But if the static variable is referenced then it gets unloaded with the unloading of the application.

To add to what Prabhu said :
Each web application has its own classloader (following the delegation model of classloading.), so when you bring down an application context , that classloader specific to that application also dies and everyting loaded using that classloaded is unloaded.


Rahul Bhattacharjee
LinkedIn - Blog
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: class variable life cycle