• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

class variable life cycle

 
FY Hsieh
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 65216
95
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It just follows standard Java rules. When the class is loaded, the variable comes into being.
 
FY Hsieh
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 502
Java jQuery Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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,
 
Rahul Bhattacharjee
Ranch Hand
Posts: 2308
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic