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 static variable in servlet - when can we use? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "static variable in servlet - when can we use?" Watch "static variable in servlet - when can we use?" New topic
Author

static variable in servlet - when can we use?

ankur rathi
Ranch Hand

Joined: Oct 11, 2004
Posts: 3830
Is there any particular use of declaring static variable in servlet???

Because anyway, only one instance of servlet exist at any time, so what difference it makes to declaring static variable and instance variable....

Please comments...

Thanks.
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

Declaring a static variable is useful since it states the intent of the variable. The static variable can also have an effect if you have a hierarchy of servlet classes. Mostly, if I ever include a variable I make it static due to the first reason.
ankur rathi
Ranch Hand

Joined: Oct 11, 2004
Posts: 3830
Sorry, I didn't get.
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

we know that there will only be a single instance of the Servlet, and that an instance variable will act like a static variable. Therefore, rather than requiring people to know about the single instance (since many people do not) by making the variable static rather than instance, it removes any confusion in the usage. Therefore the intent of the variable is clearer and less likely to be misunderstood.

Is this a better description?
[ December 06, 2005: Message edited by: David O'Meara ]
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60768
    
  65

And of course by introducing either static or instance variables you are likely to create threading issues since the single instance of the servlet is shared amongst all threads processing requests. Tread carefully.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Ryan McGuire
Ranch Hand

Joined: Feb 18, 2005
Posts: 1006
    
    3
Originally posted by Bear Bibeault:
And of course by introducing either static or instance variables you are likely to create threading issues since the single instance of the servlet is shared amongst all threads processing requests. Tread carefully.


Exactly. Since the usual idiom is for each thread to have its own set objects, declaring variables static in a Servlet makes future programmers think, "Hmmm... I better tread carefully because these variables are shared between Threads."

(Not modifying what David and Bear have said, just emphasizing.)
Jeff Albertson
Ranch Hand

Joined: Sep 16, 2005
Posts: 1780
Just for fun: it's not true that a servlet will be instantiated at most once.
If you don't believe me, put something like this in your web.xml and verify it yourself!

<servlet>
<servlet-name>foo1</servlet-name>
<servlet-class>com.acme.FooServlet</servlet-class>
<init-param>
<param-name>bar</param-name>
<param-value>17</param-value>
</init-param>
</servlet>

<servlet>
<servlet-name>foo2</servlet-name>
<servlet-class>com.acme.FooServlet</servlet-class>
<init-param>
<param-name>bar</param-name>
<param-value>42</param-value>
</init-param>
</servlet>


There is no emoticon for what I am feeling!
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

There is one "gotcha" to look out for if you're going to declare static variables in a servlet app. It's a bit of a fringe case but is probably worth mentioning.

In containers like Tomcat, each webapp has it's own classloader for it's servlets. This means static variables declared in classes within one app will be isolated from other apps running in the same container.

Occasionally, developers/sysadmins will want to have several apps to share common libraries and may be tempted to put jar files in a common area. In Tomcat this would either be tomcat/common/lib or tomcat/shared/lib. In this case, static variables would be shared across webapps; something you may not have intended.

With hard disk space being so cheap and with so many good build tools like ANT out there, I think it is folly not to pack each app with all the needed libraries; even if this means that the same jar file exists in a dozen places on one server. Nevertheless, there will always be those who will try this. If you see yourself or your sysadmin going down this road, it's a good idea to know if you're using static variables in your servlets.


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Sathvathsan Sampath
Ranch Hand

Joined: Oct 03, 2000
Posts: 96
One more thing to watch out if your web application is marked distributable - containers might not do any change detection to servlet static variables and might not propagate that across a cluster. In such a case the change in value is seen in only one of the servers in cluster. In such case you might want to consider putting shared data in httpSession.
But please do make sure you consult your container documentation to understand how the change detection and propagation takes place.


- Sathvathsan Sampath
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: static variable in servlet - when can we use?
 
Similar Threads
System.in an object ...how ??
State true or false...
Static
difference between static n non-static?
static variable in a servlet