variables 2005-10-10 - By Frans Verhoef
Back To add on to this, I would also like to highlight that in general you wouldn't want to initialize a static variable inside a constructor. This could lead to funny unexpected results, as every time you create a new object of your class, the value of your static variable changes for all of your class instances (as a static variable is shared among all objects of the same class).
Frans
On Mon, 10 Oct 2005 11:40:56 -0700, Tim Wood wrote: >?At 07:09 AM 10/10/05, SUBSCRIBE EJB-INTEREST anonymous wrote: > >>?Hi, >>?Is there any advantage of having private,static member variable >>?(Initialized in the constructor of the class) in a non-singleton >>?class. Is so, please let me know. >> > >?To answer this, think about what that decl means: The class needs a >?single copy of the variable to use as an implementation detail. >?Preferably, the variable is fina, and, if it's a reference type, >?the object it points to is immutable. ?Then no synchro. issues. >?For clean initialization, set it up in a static block; this is a >?good thread-safe way to build singletons too (R. Johnson). > >?If the object is mutable, say some master state table for all the >?instances, then you synchronize it. Since it is private &?static, >?you don't put the synchronization in the method signatures, but >?synchronize explicitly on the object itself inside the class. > >?HTH, >?TW > >?=====================================================================>?===== To unsubscribe, send email to listserv@(protected) and >?include in the body of the message "signoff J2EE-INTEREST". ?For >?general help, send email to listserv@(protected) and include in >?the body of the message "help".
==========================================================================To unsubscribe, send email to listserv@(protected) and include in the body of the message "signoff J2EE-INTEREST". For general help, send email to listserv@(protected) and include in the body of the message "help".
|
|