This week's book giveaway is in the OCMJEA forum.
We're giving away four copies of OCM Java EE 6 Enterprise Architect Exam Guide and have Paul Allen & Joseph Bambara on-line!
See this thread for details.
The moose likes Servlets and the fly likes Static & Instance in Servlet 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 » Java » Servlets
Bookmark "Static & Instance in Servlet" Watch "Static & Instance in Servlet" New topic
Author

Static & Instance in Servlet

Sachin Ramesh Vir
Greenhorn

Joined: Sep 15, 2005
Posts: 23
What will be the differences between using Static and instance variables in the Servlet. When using with servlet, both are seems to be same. Is there any difference?
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

In practical terms, there isn't a lot of difference in their functionality because a container will have only one instance of a servlet per JVM.


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Sachin Ramesh Vir
Greenhorn

Joined: Sep 15, 2005
Posts: 23
Then when we have to use static variables and when to use instance variable. For example: There will be some speical cases in which static variable can be preferred than the Instance var(& vice versa). Is there any speical cases like that?
Pradeep bhatt
Ranch Hand

Joined: Feb 27, 2002
Posts: 8919

Originally posted by Sachin Ramesh Vir:
Then when we have to use static variables and when to use instance variable. For example: There will be some speical cases in which static variable can be preferred than the Instance var(& vice versa). Is there any speical cases like that?


If you are using Servlet pooling (in case singleThreadModel) then using static variable makes sense.Remember that SingleThreadModel is deprecated in J2EE 1.4 and also was not much of use in the earlier version. Servlet pooling is supported by weblogic server.

The container could destroy a servlet when there are no requests. In such a case using static variable will avoid object creation.

This case is very rare. So there is no need to prefer using static variables in a servlet.


Groovy
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Static & Instance in Servlet