Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Static & Instance in Servlet

 
Sachin Ramesh Vir
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Sachin Ramesh Vir
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8927
Firefox Browser Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic