<?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"
	>
<channel>
	<title>Comments on: Of Singletons and Static&#160;classes</title>
	<atom:link href="http://www.magpiebrain.com/blog/2003/09/04/of-singletons-and-static-classes/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.magpiebrain.com/blog/2003/09/04/of-singletons-and-static-classes/</link>
	<description>Sam Newman's blog</description>
	<pubDate>Wed, 15 Oct 2008 19:51:37 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.5.1</generator>
		<item>
		<title>By: Prateek Kathpal</title>
		<link>http://www.magpiebrain.com/blog/2003/09/04/of-singletons-and-static-classes/#comment-47</link>
		<dc:creator>Prateek Kathpal</dc:creator>
		<pubDate>Wed, 03 Mar 2004 18:34:43 +0000</pubDate>
		<guid isPermaLink="false">http://www.magpiebrain.com/2003/09/04/of-singletons-and-static-classes/#comment-47</guid>
		<description>"To call doSingletonMethod we simply invoke it via an instance of Singleton Class: SingletonClass.getInstance().doSingletonMethod()"

"To call doStaticMethod we simply invoke it via the class: StaticClass.doStaticMethod()"
</description>
		<content:encoded><![CDATA[<p>&#8220;To call doSingletonMethod we simply invoke it via an instance of Singleton Class: SingletonClass.getInstance().doSingletonMethod()&#8221;</p>
<p>&#8220;To call doStaticMethod we simply invoke it via the class: StaticClass.doStaticMethod()&#8221; </p>
]]></content:encoded>
	</item>
	<item>
		<title>By: TaQ</title>
		<link>http://www.magpiebrain.com/blog/2003/09/04/of-singletons-and-static-classes/#comment-46</link>
		<dc:creator>TaQ</dc:creator>
		<pubDate>Fri, 27 Feb 2004 16:27:16 +0000</pubDate>
		<guid isPermaLink="false">http://www.magpiebrain.com/2003/09/04/of-singletons-and-static-classes/#comment-46</guid>
		<description>Reading this today ... is this correct:
"To call doStaticMethod we simply invoke it via the class: SingletonClass.doStaticMethod()"
Should not be:
"To call doStaticMethod we simply invoke it via the class: StaticClass.doStaticMethod()"
I mean, not SingletonClass there, but StaticClass?</description>
		<content:encoded><![CDATA[<p>Reading this today &#8230; is this correct:<br />
&#8220;To call doStaticMethod we simply invoke it via the class: SingletonClass.doStaticMethod()&#8221;<br />
Should not be:<br />
&#8220;To call doStaticMethod we simply invoke it via the class: StaticClass.doStaticMethod()&#8221;<br />
I mean, not SingletonClass there, but StaticClass?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: charles gay</title>
		<link>http://www.magpiebrain.com/blog/2003/09/04/of-singletons-and-static-classes/#comment-45</link>
		<dc:creator>charles gay</dc:creator>
		<pubDate>Fri, 23 Jan 2004 18:45:36 +0000</pubDate>
		<guid isPermaLink="false">http://www.magpiebrain.com/2003/09/04/of-singletons-and-static-classes/#comment-45</guid>
		<description>one thing about static classes:
i suppose (tell me if i'm wrong!), that you cannot use an InvocationHandler with a Proxy Class on a static class.
this characteristic is useful when you want to intercept method calls on a specific class.

charles.</description>
		<content:encoded><![CDATA[<p>one thing about static classes:<br />
i suppose (tell me if i&#8217;m wrong!), that you cannot use an InvocationHandler with a Proxy Class on a static class.<br />
this characteristic is useful when you want to intercept method calls on a specific class.</p>
<p>charles.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Tom Thomason</title>
		<link>http://www.magpiebrain.com/blog/2003/09/04/of-singletons-and-static-classes/#comment-44</link>
		<dc:creator>Tom Thomason</dc:creator>
		<pubDate>Thu, 30 Oct 2003 17:51:07 +0000</pubDate>
		<guid isPermaLink="false">http://www.magpiebrain.com/2003/09/04/of-singletons-and-static-classes/#comment-44</guid>
		<description>I am a bit puzzled by this discussion.  I would have thought the "guidelines" would be much simpler.

If the design calls for "at most one instance" in the environment use the Singleton pattern.

If the design calls for a class (with possibly multiple instances) closely associated with the enclosing class (or the programmer is too lazy to creat another file) use "static.

A class that is both would be "static Singleton".

The internal data type distinctions above are possible "propritary" guidelins but somewhat articfical to me.

What am I missing?</description>
		<content:encoded><![CDATA[<p>I am a bit puzzled by this discussion.  I would have thought the &#8220;guidelines&#8221; would be much simpler.</p>
<p>If the design calls for &#8220;at most one instance&#8221; in the environment use the Singleton pattern.</p>
<p>If the design calls for a class (with possibly multiple instances) closely associated with the enclosing class (or the programmer is too lazy to creat another file) use &#8220;static.</p>
<p>A class that is both would be &#8220;static Singleton&#8221;.</p>
<p>The internal data type distinctions above are possible &#8220;propritary&#8221; guidelins but somewhat articfical to me.</p>
<p>What am I missing?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Doug</title>
		<link>http://www.magpiebrain.com/blog/2003/09/04/of-singletons-and-static-classes/#comment-43</link>
		<dc:creator>Doug</dc:creator>
		<pubDate>Wed, 10 Sep 2003 16:38:57 +0000</pubDate>
		<guid isPermaLink="false">http://www.magpiebrain.com/2003/09/04/of-singletons-and-static-classes/#comment-43</guid>
		<description>Yes, the lazy loading is required. Well, technically, it is "lazy initialization" that is required... the JVM is free to load the classfile into memory at any time, as long as it defers throwing any errors or exceptions until the correct "logical" moment, but none of the static data in the class can be initialized until the class is first accessed.

This is called out in Section 12.4.1 of the Java2 Language Spec, which also defines "first access" as being creation of an instance ("new"), usage of any static method, or access to any non-constant static field. Certain reflective operations also can cause class initialization.

Per the spec, "A class or interface will not be initialized under any other circumstance."

Section 12.4.1 can be found at http://java.sun.com/docs/books/jls/second_edition/html/execution.doc.html#57946</description>
		<content:encoded><![CDATA[<p>Yes, the lazy loading is required. Well, technically, it is &#8220;lazy initialization&#8221; that is required&#8230; the JVM is free to load the classfile into memory at any time, as long as it defers throwing any errors or exceptions until the correct &#8220;logical&#8221; moment, but none of the static data in the class can be initialized until the class is first accessed.</p>
<p>This is called out in Section 12.4.1 of the Java2 Language Spec, which also defines &#8220;first access&#8221; as being creation of an instance (&#8220;new&#8221;), usage of any static method, or access to any non-constant static field. Certain reflective operations also can cause class initialization.</p>
<p>Per the spec, &#8220;A class or interface will not be initialized under any other circumstance.&#8221;</p>
<p>Section 12.4.1 can be found at <a href="http://java.sun.com/docs/books/jls/second_edition/html/execution.doc.html#57946"  rel="nofollow">http://java.sun.com/docs/books/jls/second_edition/html/execution.doc.html#57946</a></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Sam Newman</title>
		<link>http://www.magpiebrain.com/blog/2003/09/04/of-singletons-and-static-classes/#comment-42</link>
		<dc:creator>Sam Newman</dc:creator>
		<pubDate>Tue, 09 Sep 2003 22:45:26 +0000</pubDate>
		<guid isPermaLink="false">http://www.magpiebrain.com/2003/09/04/of-singletons-and-static-classes/#comment-42</guid>
		<description>Who would of thought there could be so many nuances to the Java implementation of one of the simplest Java patterns of all? Personally I'm hoping for a Singleton metadata keyword!</description>
		<content:encoded><![CDATA[<p>Who would of thought there could be so many nuances to the Java implementation of one of the simplest Java patterns of all? Personally I&#8217;m hoping for a Singleton metadata keyword!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: damien</title>
		<link>http://www.magpiebrain.com/blog/2003/09/04/of-singletons-and-static-classes/#comment-41</link>
		<dc:creator>damien</dc:creator>
		<pubDate>Tue, 09 Sep 2003 18:25:02 +0000</pubDate>
		<guid isPermaLink="false">http://www.magpiebrain.com/2003/09/04/of-singletons-and-static-classes/#comment-41</guid>
		<description>False.  That's a good one.  

Because the 'B' class is loaded in the 'A' constructor, 'B' statically initializes it's 'A' variable.  The singleton object hasn't been created yet (it hasn't returned from the original call to the constructor), therefore 'A's instance variable is still null.  End result is you get two 'A' objects in memory.

The better way to do it is like this:

....
private static A instance = new A();
public static A getInstance() {
  return instance;
}
....


You still get a "false" printing out, because the references are different.  However, only one object is created - this preserves the singleton.</description>
		<content:encoded><![CDATA[<p>False.  That&#8217;s a good one.  </p>
<p>Because the &#8216;B&#8217; class is loaded in the &#8216;A&#8217; constructor, &#8216;B&#8217; statically initializes it&#8217;s &#8216;A&#8217; variable.  The singleton object hasn&#8217;t been created yet (it hasn&#8217;t returned from the original call to the constructor), therefore &#8216;A&#8217;s instance variable is still null.  End result is you get two &#8216;A&#8217; objects in memory.</p>
<p>The better way to do it is like this:</p>
<p>....<br />
private static A instance = new A();<br />
public static A getInstance() {</p>
<p>  return instance;<br />
}<br />
....</p>
<p>You still get a &#8220;false&#8221; printing out, because the references are different.  However, only one object is created &#8211; this preserves the singleton.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Weiqi Gao</title>
		<link>http://www.magpiebrain.com/blog/2003/09/04/of-singletons-and-static-classes/#comment-40</link>
		<dc:creator>Weiqi Gao</dc:creator>
		<pubDate>Tue, 09 Sep 2003 13:32:41 +0000</pubDate>
		<guid isPermaLink="false">http://www.magpiebrain.com/2003/09/04/of-singletons-and-static-classes/#comment-40</guid>
		<description>Guess what is printed by Main:

// Main.java
class A {
    // A is a singleton.  The getInstance() method is even synchronized.
    private static A instance;
    public synchronized static A getInstance() {
        return (instance == null) ? (instance = new A()) : instance;
    }

    // A uses B
    private B b;
    private A() {
        b = new B();
    }
}

class B {
    // B caches the only instance of A
    public static A a = A.getInstance();
}

class Main {
    public static void main(String[] args) {
        System.out.println(A.getInstance() == B.a);
    }
}</description>
		<content:encoded><![CDATA[<p>Guess what is printed by Main:</p>
<p>// Main.java<br />
class A {</p>
<p>    // A is a singleton.  The getInstance() method is even synchronized.<br />
    private static A instance;<br />
    public synchronized static A getInstance() {<br />
        return (instance  null) &#63; (instance &#61; new A()) : instance;<br />
    }</p>
<p>    // A uses B<br />
    private B b;<br />
    private A() {<br />
        b &#61; new B();<br />
    }<br />
}</p>
<p>class B {</p>
<p>    // B caches the only instance of A<br />
    public static A a &#61; A.getInstance();<br />
}</p>
<p>class Main {</p>
<p>    public static void main(String[] args) {<br />
        System.out.println(A.getInstance()  B.a);<br />
    }<br />
}</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Sam Newman</title>
		<link>http://www.magpiebrain.com/blog/2003/09/04/of-singletons-and-static-classes/#comment-39</link>
		<dc:creator>Sam Newman</dc:creator>
		<pubDate>Tue, 09 Sep 2003 12:00:55 +0000</pubDate>
		<guid isPermaLink="false">http://www.magpiebrain.com/2003/09/04/of-singletons-and-static-classes/#comment-39</guid>
		<description>In my code i tend to use an underscore prefix for all instance variables - I have seen some people using understores as a suffix. Using an m_varname as  opposed to an _varname is a lot more ugly in my opinion, but as always beauty is in the eye of the beholder!</description>
		<content:encoded><![CDATA[<p>In my code i tend to use an underscore prefix for all instance variables &#8211; I have seen some people using understores as a suffix. Using an m_varname as  opposed to an _varname is a lot more ugly in my opinion, but as always beauty is in the eye of the beholder!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: damien</title>
		<link>http://www.magpiebrain.com/blog/2003/09/04/of-singletons-and-static-classes/#comment-38</link>
		<dc:creator>damien</dc:creator>
		<pubDate>Tue, 09 Sep 2003 04:54:37 +0000</pubDate>
		<guid isPermaLink="false">http://www.magpiebrain.com/2003/09/04/of-singletons-and-static-classes/#comment-38</guid>
		<description>Instance variables starting with "m_"...  That brings me back to my Visual C++ days.</description>
		<content:encoded><![CDATA[<p>Instance variables starting with &#8220;m_&#8221;...  That brings me back to my Visual C++ days.</p>
]]></content:encoded>
	</item>
</channel>
</rss>
