Re: PRIVATE VARIABLES WITHIN PUBLIC CLASS
Chris Pratt <[email protected]> Mon, 6 Feb 2006 14:19:04 -0800
| Newsgroups | gmane.comp.java.sun.servlet |
|---|---|
| Message-ID | <[email protected]> |
------=_Part_2678_21807220.1139264344930 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable Content-Disposition: inline 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 =3D 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=3Dabc2.variablename=3Dabc3.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 reference= d 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 unti= l 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 ------=_Part_2678_21807220.1139264344930 Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable Content-Disposition: inline <br><br><div><span class=3D"gmail_quote">On 2/6/06, <b class=3D"gmail_sende= rname">VERONICA ROACH</b> <<a href=3D"mailto:[email protected]">vcragai= [email protected]</a>> wrote:</span><blockquote class=3D"gmail_quote" style=3D= "border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padd= ing-left: 1ex;"> 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 blam= e !! - <br>if I invoke a new object with Objectname abc =3D new Objectname ;<br>- = suppose I have to create 6 of these -<br> these objects would ea= ch own their own private variables -<br> ie : I could reference abc.variabl= ename and=20 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 )</bloc= kquote><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 &= quot;instance". Every time you invoke the new operator you are c= reating a new instance of your class, which gets new space set aside to hol= d it's instance variables. <br></div><br><blockquote class=3D"gmail_quote" style=3D"border-left: 1px s= olid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">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=3Dabc2.varia= blename=3Dabc3.variablename if they get<br>invoked at the same moment = ?</blockquote><div><br>In Servlets there is only ever 1 instance of y= our Servlet Class. In other words the Servlet Container will only eve= r 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. = =20 <br>As you see both of these descriptions are saying the same thing, = instance variables are owned by the instance that created them. In th= e case of a Servlet, the difference is there's only one instance.<br></div>= <br><blockquote class=3D"gmail_quote" style=3D"border-left: 1px solid rgb(2= 04, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;"> if so - I dont see how static class variables then are different<br>from o= rdinary 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 Cl= ass ever instantiated. In normal (i.e. non-Servlet) programming the d= ifference is that Class Variables belong to the class (there's only ever on= e of these in a program as well) and must be referenced using the=20 Class.variable reference. Instance Variables belong to each instance = and must be referenced using instance.variable.<br></div><br><blockquote cl= ass=3D"gmail_quote" style=3D"border-left: 1px solid rgb(204, 204, 204); mar= gin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;"> 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 u= nderstand. <br> (*Chris*)<br> </div><br><blockquote class=3D"gmail_quote" s= tyle=3D"border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8e= x; padding-left: 1ex;">thanks -<br>vcragain<br></blockquote></div><br> ___________________________________________________________________________ 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> ------=_Part_2678_21807220.1139264344930--