> Please read the following fascinating (and intense)
> discussion on the Singleton
> at:
>
> http://www.javaspecialists.co.za/archive/Issue052.html,
>
> and
>
> http://www.cs.umd.edu/~pugh/java/memoryModel/DoubleCheckedLock
> ing.html,
> to understand why synchronizing creation of the Singleton
> doesn't always work.

(Quote from article)
-------------------------------------------------
Making it work for static singletons
If the singleton you are creating is static (i.e., there will only be
one Helper created), as opposed to a property of another object (e.g.,
there will be one Helper for each Foo object, there is a simple and
elegant solution.

Just define the singleton as a static field in a separate class. The
semantics of Java guarantee that the field will not be initialized until
the field is referenced, and that any thread which accesses the field
will see all of the writes resulting from initializing that field.

class HelperSingleton {
  static Helper singleton = new Helper();
  }
--------------------------------------------------

Doesn't the text above imply that what I asked earlier is correct? That
if you instead of creating the Singleton in the getInstance() method
declared it as static field in the Singleton class itself forces it to
be fully initialized before it is referenced?

I don't really understand why declaring the field in a separate helper
class guarantees correct initialization? The code above almost seems
like a spelling error to me and that the class really should be named
Helper instead of HelperSingleton. Which then instead makes the static
field self contained in it's own Class. Like in the example below.

(Quote from my earlier example)
----------------------------------------------------------
public class MySingleton {
   private static MySingleton instance= new MySingleton();
   ...
}
----------------------------------------------------------

Or am I wrong?

===========================================================================
To unsubscribe: mailto [EMAIL PROTECTED] with body: "signoff JSP-INTEREST".
For digest: mailto [EMAIL PROTECTED] with body: "set JSP-INTEREST DIGEST".

Some relevant archives, FAQs and Forums on JSPs can be found at:

 http://java.sun.com/products/jsp
 http://archives.java.sun.com/jsp-interest.html
 http://forums.java.sun.com
 http://www.jspinsider.com

Reply via email to