[concurrency-interest] Static initializer

Mike Quilleash mike.quilleash at subexazure.com
Tue Apr 10 09:07:57 EDT 2007

Hi all,
Just a quick question about static initializers.  I've read JCiP
(excellent book BTW) and wondered if the following is threadsafe.  The
examples in the book didn't quite cover this case (code/method calls in
the constructor).
public class MyClass
   private static MyClass instance = new MyClass();
   private final Map map;
   private MyClass()
      // do some init work
      map = Collections.unmodifiableMap( ... );
   public static MyClass getInstance()
       return instance;
   // other accessor method(s) to access info in the map
Is any thread that calls getInstance() guaranteed to see a fully
initialized MyClass instance?  If so, is this because of the static
initialization, or the "final" someObject, or both?  I'd like to know
before I go and use this pattern everywhere in my code!

 This e-mail is bound by the terms and conditions described at http://www.subexazure.com/mail-disclaimer.html

-------------- next part --------------
An HTML attachment was scrubbed...
URL: /pipermail/attachments/20070410/bfa84cab/attachment.html 

More information about the Concurrency-interest mailing list