• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Instance Vs Static in Servlet

 
Ranch Hand
Posts: 200
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I want to know the differences between Static variable and Instance variable in servlet.

Static variable: One per class
Instance variable: One per Instance

In servlet: A servlet that does not declare SingleThreadModel usually has one and only one instance, shared among all concurrent requests hitting that servlet.

So it seems that both instance variable and static variable behaves same in the servlet.

But I got the below defn from the website, which can't understand. Could anybody expain it with one simple example:


The big difference between instance variables and static variables comes when you have configured your servlet engine to instantiate two instances of the same servlet class how?(two instances of same servlet), but with different init parameters. In this case, there will be two instances of the same servlet class, which means two sets of instance variables, but only one set of static variables.

 
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It is typically better not to use either instance or static variables. Part of the answer about 'multiple instances' can be found in this thread.
 
Mark Henryson
Ranch Hand
Posts: 200
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I couldn't understand from that post(the link given by you).

Sure, I will avoid static and instance var in my program. But why? I just want to know the reason.
 
Ranch Hand
Posts: 160
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In servlet: A servlet that does not declare SingleThreadModel usually has one and only one instance, shared among all concurrent requests hitting that servlet.

that means if a servlet implements single thread model
than for every request there will be a differnt instance

and for the static and instance variable:

if we are using static variable than you have to synchronize it
because static has only one copy

i think this will sortout your some part of problem
 
Ranch Hand
Posts: 266
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Mark,

Even if you dont' implement the SingleThreadModel, you can make the container create multiple instances of a servlet class by having multiple servlet declarations for the same servlet class. For example if you have:



then the container will create two instances of MyServletClass. The thread pointed to by David discusses in part why we may need to have multiple servlet decalarations for the same servlet class.
[ September 27, 2005: Message edited by: Satish Chilukuri ]
reply
    Bookmark Topic Watch Topic
  • New Topic