<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
		>
<channel>
	<title>Comments on: Java Singleton</title>
	<atom:link href="http://www.chiaocheng.com/java-singleton/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.chiaocheng.com</link>
	<description>Just another WordPress weblog</description>
	<lastBuildDate>Wed, 13 Jan 2010 02:19:58 -0800</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
		<item>
		<title>By: chiao</title>
		<link>http://www.chiaocheng.com/java-singleton/comment-page-1/#comment-181</link>
		<dc:creator>chiao</dc:creator>
		<pubDate>Mon, 19 Oct 2009 17:47:20 +0000</pubDate>
		<guid isPermaLink="false">http://www.chiaocheng.com/?page_id=123#comment-181</guid>
		<description>It will work either way.  Any static variables inside the class will be guaranteed to be initialized only once and before they are used.  Having other static variables inside the Singleton class does not affect the static &quot;instance&quot; variable inside the inner class.  Of course, your variable &quot;example&quot; may not be thread-safe depending on how you use it.  But &quot;instance&quot; is still thread safe.</description>
		<content:encoded><![CDATA[<p>It will work either way.  Any static variables inside the class will be guaranteed to be initialized only once and before they are used.  Having other static variables inside the Singleton class does not affect the static &#8220;instance&#8221; variable inside the inner class.  Of course, your variable &#8220;example&#8221; may not be thread-safe depending on how you use it.  But &#8220;instance&#8221; is still thread safe.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: David</title>
		<link>http://www.chiaocheng.com/java-singleton/comment-page-1/#comment-177</link>
		<dc:creator>David</dc:creator>
		<pubDate>Sat, 17 Oct 2009 23:37:52 +0000</pubDate>
		<guid isPermaLink="false">http://www.chiaocheng.com/?page_id=123#comment-177</guid>
		<description>So if you have the same implementation as Initialize-on-demand with the difference being that class Singleton declares a static variable, than this approach wont work and it will not be thread safe ? For example:

public class Singleton{
	
	 private static final int example = 0; //static variable declared just for example
	 
        private Singleton(){ }

         public static Singleton getInstance() {
		 
		  return SingletonHolder.instance;
	 }
	
	 private static class SingletonHolder { 
		 static final Singleton instance = new Singleton();
	 }

Thanks for your response.</description>
		<content:encoded><![CDATA[<p>So if you have the same implementation as Initialize-on-demand with the difference being that class Singleton declares a static variable, than this approach wont work and it will not be thread safe ? For example:</p>
<p>public class Singleton{</p>
<p>	 private static final int example = 0; //static variable declared just for example</p>
<p>        private Singleton(){ }</p>
<p>         public static Singleton getInstance() {</p>
<p>		  return SingletonHolder.instance;<br />
	 }</p>
<p>	 private static class SingletonHolder {<br />
		 static final Singleton instance = new Singleton();<br />
	 }</p>
<p>Thanks for your response.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: chiao</title>
		<link>http://www.chiaocheng.com/java-singleton/comment-page-1/#comment-174</link>
		<dc:creator>chiao</dc:creator>
		<pubDate>Fri, 16 Oct 2009 09:01:30 +0000</pubDate>
		<guid isPermaLink="false">http://www.chiaocheng.com/?page_id=123#comment-174</guid>
		<description>Yes, multiple threads can call getInstance() at the same time.  But just like Eager Initialization, the thread safety comes from the fact that the singleton instance is ultimately held in a static variable.  The JVM guarantees that static variables are only initialized once by the classloader, thus insuring that all threads calling getInstance() will return the same instance of the variable.

The advantage for initialize-on-demand idiom is that the static variable is inside an inner class.  The main Singleton class does not contain any static variables so eager instantiation will not take place.</description>
		<content:encoded><![CDATA[<p>Yes, multiple threads can call getInstance() at the same time.  But just like Eager Initialization, the thread safety comes from the fact that the singleton instance is ultimately held in a static variable.  The JVM guarantees that static variables are only initialized once by the classloader, thus insuring that all threads calling getInstance() will return the same instance of the variable.</p>
<p>The advantage for initialize-on-demand idiom is that the static variable is inside an inner class.  The main Singleton class does not contain any static variables so eager instantiation will not take place.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: David</title>
		<link>http://www.chiaocheng.com/java-singleton/comment-page-1/#comment-167</link>
		<dc:creator>David</dc:creator>
		<pubDate>Thu, 15 Oct 2009 19:03:00 +0000</pubDate>
		<guid isPermaLink="false">http://www.chiaocheng.com/?page_id=123#comment-167</guid>
		<description>For initialize on demand, isn&#039;t there the possibility that more than one thread may call getInstance() at the same time ?</description>
		<content:encoded><![CDATA[<p>For initialize on demand, isn&#8217;t there the possibility that more than one thread may call getInstance() at the same time ?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: chiao</title>
		<link>http://www.chiaocheng.com/java-singleton/comment-page-/#comment-109</link>
		<dc:creator>chiao</dc:creator>
		<pubDate>Wed, 23 Sep 2009 20:41:16 +0000</pubDate>
		<guid isPermaLink="false">http://www.chiaocheng.com/?page_id=123#comment-109</guid>
		<description>Yup, that article talks about the possibility of using reflection to access private constructors in java.  By doing so, you can circumvent the private access restriction.  So there are ways to break your singleton pattern but if you wanted to break your own code, there are easier ways to do it.  The only time this would be a problem in the real world would be a framework or something trying to access your singleton through reflection.  That generally will not happen since a singleton by nature would be fairly custom.  

I personally have not seen anyone trying to hack their own singletons on purpose.  Why go through the trouble of creating a singleton only to hack it into multiple instances?  Might as well just create a regular class in the first place.

So that article is not really a contradiction of thread-safe methods for creating singletons.  The methods listed here are still thread-safe methods of creating a singleton.  But it does not prevent you from using reflection to hack the singleton into multiple instances.</description>
		<content:encoded><![CDATA[<p>Yup, that article talks about the possibility of using reflection to access private constructors in java.  By doing so, you can circumvent the private access restriction.  So there are ways to break your singleton pattern but if you wanted to break your own code, there are easier ways to do it.  The only time this would be a problem in the real world would be a framework or something trying to access your singleton through reflection.  That generally will not happen since a singleton by nature would be fairly custom.  </p>
<p>I personally have not seen anyone trying to hack their own singletons on purpose.  Why go through the trouble of creating a singleton only to hack it into multiple instances?  Might as well just create a regular class in the first place.</p>
<p>So that article is not really a contradiction of thread-safe methods for creating singletons.  The methods listed here are still thread-safe methods of creating a singleton.  But it does not prevent you from using reflection to hack the singleton into multiple instances.</p>
]]></content:encoded>
	</item>
</channel>
</rss>
