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.