static variables in web applications versus standalone application

M

Matt

When we declare static variable in a standalone application,
the lifetime of a static variable begins from the execution of the program
untill the termination of the program.

When we declare static variable in a web application,
the lifetime of a static variable begins from the the start of the web application
in web server, untill the web application is stopped in web server.
At first, I thought the lifetime of a static variable in a web application is
from opening the web browser until the web browser closes, that is session
lifetime, and now I realize it is wrong.

Please confirm the concept. Thanks
 
T

Tony Morris

Matt said:
When we declare static variable in a standalone application,
the lifetime of a static variable begins from the execution of the program
untill the termination of the program.

When we declare static variable in a web application,
the lifetime of a static variable begins from the the start of the web application
in web server, untill the web application is stopped in web server.
At first, I thought the lifetime of a static variable in a web application is
from opening the web browser until the web browser closes, that is session
lifetime, and now I realize it is wrong.

Please confirm the concept. Thanks

The 'lifetime' of a static member is the same as the class that encapsulates
it.
The 'lifetime' of a class is from the moment it is loaded from the time it
is unloaded.
A class is loaded "on-demand" as per VM Specification.
A class is unloaded when the VM system class loader cache dies, which is at
VM exit time.

A session variable is explained in the J2EE tutorial.
Note that it is not associated with a VM, class or class loader in the same
way a static field is.
It is also superficial in that it has nothing to do with your web browser -
it can be thought of as being associated with one or more requests, which is
independant of a browser.

Time to do some reading.
 
F

fanfaron

The static variable dies in a standalone application, because the
garbage collector cleans all your object because you application has
ended.

But if you are using this in a servlet or jsp, your application is not
dead until your web server dies. So even if you closes the web browser,
your web server is not close. Try to shut down your tomcat and you
variable will be no more.

hope this help
 
M

Mike Schilling

Tony Morris said:
The 'lifetime' of a static member is the same as the class that
encapsulates
it.
The 'lifetime' of a class is from the moment it is loaded from the time it
is unloaded.
A class is loaded "on-demand" as per VM Specification.
A class is unloaded when the VM system class loader cache dies, which is
at
VM exit time.

A class is unloaded when the classloader which loaded it is unloaded. If
this is a web-application-specific classloader, this may happen when the
application exits or sometime thereafter. Ot it may not, depending both on
the VM implementation and on the container's classloader implementation.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top