It is useful to note that it is theoretically possible for two competing Threads to enter the setData method and get past the "if (! isInitialized)" statement before one of them changes the value to true, causing possible data corruption. This is why I prefer to initialize singletons either in a static initializer or at the time of declaration. Practically of course, there may be no such threat at all.

Erik


Ashish Kulkarni wrote:

Hi
that is what i am going to do,
hope it is a good practise
Ashish
--- Cedric Levieux <[EMAIL PROTECTED]> wrote:



public MyClass
{
private boolean isInitialzed = false;
private static String data;
 public static String getData()
 {
   return data;
 }
 public static setData(String input)
 {
   if (! isInitialzed)
   {
       data = input;
       isInitialzed = true;
   }
 }

}
----- Original Message ----- From: "Ashish Kulkarni" <[EMAIL PROTECTED]>
To: "Struts Users Mailing List"
<user@struts.apache.org>
Sent: Wednesday, January 26, 2005 8:15 PM
Subject: Re: [OT] a java question





so it will be some thing like this

public MyClass
{
private static String data;
 public static String getData()
 {
   return data;
 }
 public static setData(String input)
 {
   data = input;
 }

}
this will work, but how to make sure that setData


is


called only from initalization class, and no one


else


calls this method , how to protect this method


from


being called by other classes

Ashish
--- PA <[EMAIL PROTECTED]> wrote:



On Jan 26, 2005, at 20:08, Ashish Kulkarni


wrote:


how do i protect it from updated
by any other class.


Just don't. Use a static accessor method


instead.


Cheers

--
PA
http://alt.textdrive.com/





---------------------------------------------------------------------


To unsubscribe, e-mail:
[EMAIL PROTECTED]
For additional commands, e-mail:
[EMAIL PROTECTED]






__________________________________ Do you Yahoo!? Yahoo! Mail - Find what you need with new enhanced


search.


http://info.mail.yahoo.com/mail_250




---------------------------------------------------------------------


To unsubscribe, e-mail:


[EMAIL PROTECTED]


For additional commands, e-mail:


[EMAIL PROTECTED]




---------------------------------------------------------------------


To unsubscribe, e-mail:
[EMAIL PROTECTED]
For additional commands, e-mail:
[EMAIL PROTECTED]






=====
A$HI$H



__________________________________ Do you Yahoo!? Yahoo! Mail - now with 250MB free storage. Learn more.
http://info.mail.yahoo.com/mail_250


---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]





--------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]



Reply via email to