Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Depending on the usage, there are several "correct" answers.</p> <p>Since java5 the best way to do it is to use an enum:</p> <pre><code>public enum Foo { INSTANCE; } </code></pre> <p>Pre java5, the most simple case is:</p> <pre><code>public final class Foo { private static final Foo INSTANCE = new Foo(); private Foo() { if (INSTANCE != null) { throw new IllegalStateException("Already instantiated"); } } public static Foo getInstance() { return INSTANCE; } public Object clone() throws CloneNotSupportedException{ throw new CloneNotSupportedException("Cannot clone instance of this class"); } } </code></pre> <p>Let's go over the code. First, you want the class to be final. In this case, I've used the <code>final</code> keyword to let the users know it is final. Then you need to make the constructor private to prevent users to create their own Foo. Throwing an exception from the constructor prevents users to use reflection to create a second Foo. Then you create a <code>private static final Foo</code> field to hold the only instance, and a <code>public static Foo getInstance()</code> method to return it. The Java specification makes sure that the constructor is only called when the class is first used.</p> <p>When you have a very large object or heavy construction code AND also have other accessible static methods or fields that might be used before an instance is needed, then and only then you need to use lazy initialization.</p> <p>You can use a <code>private static class</code> to load the instance. The code would then look like:</p> <pre><code>public final class Foo { private static class FooLoader { private static final Foo INSTANCE = new Foo(); } private Foo() { if (FooLoader.INSTANCE != null) { throw new IllegalStateException("Already instantiated"); } } public static Foo getInstance() { return FooLoader.INSTANCE; } } </code></pre> <p>Since the line <code>private static final Foo INSTANCE = new Foo();</code> is only executed when the class FooLoader is actually used, this takes care of the lazy instantiation, and is it guaranteed to be thread safe.</p> <p>When you also want to be able to serialize your object you need to make sure that deserialization won't create a copy.</p> <pre><code>public final class Foo implements Serializable { private static final long serialVersionUID = 1L; private static class FooLoader { private static final Foo INSTANCE = new Foo(); } private Foo() { if (FooLoader.INSTANCE != null) { throw new IllegalStateException("Already instantiated"); } } public static Foo getInstance() { return FooLoader.INSTANCE; } @SuppressWarnings("unused") private Foo readResolve() { return FooLoader.INSTANCE; } } </code></pre> <p>The method <code>readResolve()</code> will make sure the only instance will be returned, even when the object was serialized in a previous run of your program.</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. 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