Re: PRIVATE VARIABLES WITHIN PUBLIC CLASS
Eric Noriega <[email protected]> Mon, 6 Feb 2006 18:56:54 -0500
| Newsgroups | gmane.comp.java.sun.servlet |
|---|---|
| Organization | The George Washington University |
| Message-ID | <[email protected]> |
This is a multi-part message in MIME format.
--------------050101020804030908070604
Content-Type: text/plain; charset=us-ascii; format=flowed
Content-Transfer-Encoding: 7bit
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]
> <mailto:[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
--------------050101020804030908070604
Content-Type: text/html; charset=us-ascii
Content-Transfer-Encoding: 7bit
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta content="text/html;charset=ISO-8859-1" http-equiv="Content-Type">
<title></title>
</head>
<body bgcolor="#ffffff" text="#000000">
<br>
No, that's not correct. The number of servlet class instances is
server dependent, and you may have any number of classes created
automatically.<br>
<br>
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.<br>
<br>
Private is an inheritance mechanism to keep subclasses from gaining
access to super class variables, and has nothing to do with runtime
access.<br>
<br>
<br>
Chris Pratt wrote:
<blockquote
cite="[email protected]"
type="cite">
<meta http-equiv="Context-Type"
content="text/html; charset=ISO-8859-1">
<br>
<br>
<div><span>On 2/6/06, <b>VERONICA ROACH</b> <<a
href="mailto:[email protected]">[email protected]</a>> wrote:</span>
<blockquote>hello - thank you so much for your response - I now
realise what I<br>
have to do with servlets.<br>
<br>
I had been using the following assumptions in a non-servlet<br>
environment - read somewhere - cant find which book to blame !! -
<br>
if I invoke a new object with Objectname abc = new Objectname ;<br>
- suppose I have to create 6 of these -<br>
these objects would each own their own private variables -<br>
ie : I could reference abc.variablename and abc2.variablename etc<br>
( unless the static keyword was used in which case java would<br>
maintain 1 variable in memory for the class variable )</blockquote>
<div><br>
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.
<br>
</div>
<br>
<blockquote>Now based on your response - I still have to make sure I
synchronize<br>
access to the private variables wherever multiples of the class may
<br>
get invoked at the same time - that is<br>
abc.variablename=abc2.variablename=abc3.variablename if they get<br>
invoked at the same moment ?</blockquote>
<div><br>
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. <br>
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.<br>
</div>
<br>
<blockquote> if so - I dont see how static class variables then are
different<br>
from ordinary class level private variables ???</blockquote>
<div><br>
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.<br>
</div>
<br>
<blockquote>sorry to be a pain - I thought I had this nailed down but
to my<br>
disgust my servlets stamped on each other (:>)</blockquote>
<div><br>
I hope that clears it up. If not, keep asking and I'll keep answering
until you understand.
<br>
(*Chris*)<br>
</div>
<br>
<blockquote>thanks -<br>
vcragain<br>
</blockquote>
</div>
<br>
___________________________________________________________________________
To unsubscribe, send email to <a class="moz-txt-link-abbreviated" href="mailto:[email protected]">[email protected]</a> and include in the
body
of the message "signoff SERVLET-INTEREST".
<p>Archives:
<a class="moz-txt-link-freetext" href="http://archives.java.sun.com/archives/servlet-interest.html">http://archives.java.sun.com/archives/servlet-interest.html</a>
Resources: <a class="moz-txt-link-freetext" href="http://java.sun.com/products/servlet/external-resources.html">http://java.sun.com/products/servlet/external-resources.html</a>
LISTSERV Help: <a class="moz-txt-link-freetext" href="http://www.lsoft.com/manuals/user/user.html">http://www.lsoft.com/manuals/user/user.html</a>
</p>
<p></p>
</blockquote>
</body>
</html>
___________________________________________________________________________
To unsubscribe, send email to [email protected] and include in the body
of the message "signoff SERVLET-INTEREST".
<p>
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
<p>
--------------050101020804030908070604--