Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <blockquote> <p>while a singleton is instantiated by a static method</p> </blockquote> <p>While this is the usual way of doing it, this is by no means the only way.</p> <p>In Java 1.5 a new version of Singleton is the enum singleton pattern:</p> <pre><code>public enum Elvis{ INSTANCE // this is a singleton, no static methods involved } </code></pre> <p>And since enums can have constructors, methods and fields, you can give them all the immutable state you want.</p> <p><strong>Reference:</strong> </p> <ul> <li><a href="http://download.oracle.com/javase/tutorial/java/javaOO/enum.html" rel="nofollow">Java tutorial: Enum Types</a></li> <li><a href="http://java.sun.com/docs/books/effective/" rel="nofollow" title="You must!!! read this book">Effective Java</a>, Item 3</li> <li><a href="http://en.wikipedia.org/wiki/Singleton_pattern#The_Enum-way" rel="nofollow" title="The Enum Singleton Pattern">Singleton (the enum way)</a> (WikiPedia)</li> </ul> <hr> <p>Also, the term Singleton leaves some room for interpretation. Singleton means that there is exactly one object per defined scope, but the scope can be a number of things:</p> <ul> <li><s>Java VM</s> <strong>Classloader</strong> (thanks @Paŭlo Ebermann for reminding me): in this case use <a href="http://en.wikipedia.org/wiki/Singleton_pattern#The_Enum-way" rel="nofollow" title="The Enum Singleton Pattern">enums</a> or the <a href="http://en.wikipedia.org/wiki/Initialization_on_demand_holder_idiom#Example_Java_Implementation" rel="nofollow" title="aka: Initialization on demand holder idiom">initialize-through-static-inner-class pattern</a>. This is of course what is usually meant by a singleton.<br> Be Careful: enums and all other singletons are broken if loaded through multiple Classloaders.</li> <li><strong>Enterprise Application</strong> (in this case you need a container-managed singleton, e.g. a <a href="http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/beans.html#beans-factory-scopes-singleton" rel="nofollow" title="Spring 3.x Reference: The Singleton scope">Spring singleton bean</a>). This can be several objects per VM or one object per several VMs (or one Object per VM, of course)</li> <li><strong>Thread</strong> (use a <a href="http://download.oracle.com/javase/6/docs/api/java/lang/ThreadLocal.html" rel="nofollow"><code>ThreadLocal</code></a>)</li> <li><strong>Request</strong> / <strong>Session</strong> (again, you'll need a container to manage this, <a href="http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/beans.html#beans-factory-scopes-other" rel="nofollow" title="Spring 3.x Reference: Request, session, and global session scopes">Spring</a>, <a href="http://docs.jboss.org/seam/2.2.1.CR1/reference/en-US/html/concepts.html#d0e3960" rel="nofollow" title="Seam Reference: Defining the component scope">Seam</a> and several others can do that for you)</li> <li>did I forget anything?</li> </ul> <p>All of the above can be made immutable, each in their own way (although it's usually not easy for container-managed components)</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload