No, that's not correct. The number of servlet class instances is
server dependent, and you may have any number of classes created
automatically.
Static variables are global to all instances of the class, but
since you are in a multi-threaded environment it doesn't matter
anyway. Variables in the servlet class need to be thread safe, as they
are global to the class and can be accessed by more than one request at
a time.
Private is an inheritance mechanism to keep subclasses from gaining
access to super class variables, and has nothing to do with runtime
access.
Chris Pratt wrote:
On 2/6/06, VERONICA ROACH <[EMAIL PROTECTED]> wrote:
hello - thank you so much for your response - I now
realise what I
have to do with servlets.
I had been using the following assumptions in a non-servlet
environment - read somewhere - cant find which book to blame !! -
if I invoke a new object with Objectname abc = new Objectname ;
- suppose I have to create 6 of these -
these objects would each own their own private variables -
ie : I could reference abc.variablename and abc2.variablename etc
( unless the static keyword was used in which case java would
maintain 1 variable in memory for the class variable )
That is correct each instance of the class has it's own set of instance
variables (the ones not marked static). The key word here is
"instance". Every time you invoke the new operator you are creating a
new instance of your class, which gets new space set aside to hold it's
instance variables.
Now based on your response - I still have to make sure I
synchronize
access to the private variables wherever multiples of the class may
get invoked at the same time - that is
abc.variablename=abc2.variablename=abc3.variablename if they get
invoked at the same moment ?
In Servlets there is only ever 1 instance of your Servlet Class. In
other words the Servlet Container will only ever call the new operator
on your class once and it will use that instance to service all the
requests that come in for your servlet's services.
As you see both of these descriptions are saying the same thing,
instance variables are owned by the instance that created them. In the
case of a Servlet, the difference is there's only one instance.
if so - I dont see how static class variables then are
different
from ordinary class level private variables ???
In a Servlet there's almost no difference between static (i.e. Class
Variables) and non-static (
i.e. Instance Variables). Because there's only one Instance of the
Class ever instantiated. In normal (i.e. non-Servlet) programming the
difference is that Class Variables belong to the class (there's only
ever one of these in a program as well) and must be referenced using
the Class.variable reference. Instance Variables belong to each
instance and must be referenced using instance.variable.
sorry to be a pain - I thought I had this nailed down but
to my
disgust my servlets stamped on each other (:>)
I hope that clears it up. If not, keep asking and I'll keep answering
until you understand.
(*Chris*)
thanks -
vcragain
___________________________________________________________________________
To unsubscribe, send email to [EMAIL PROTECTED] and include in the
body
of the message "signoff SERVLET-INTEREST".
Archives:
http://archives.java.sun.com/archives/servlet-interest.html
Resources: http://java.sun.com/products/servlet/external-resources.html
LISTSERV Help: http://www.lsoft.com/manuals/user/user.html
___________________________________________________________________________
To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
of the message "signoff SERVLET-INTEREST".
Archives: http://archives.java.sun.com/archives/servlet-interest.html
Resources: http://java.sun.com/products/servlet/external-resources.html
LISTSERV Help: http://www.lsoft.com/manuals/user/user.html
|