Singletonpublic class Singleton extends Object An example of a Singleton implementation in Java. |
Fields Summary |
---|
private static Singleton | singleton | protected String | name |
Constructors Summary |
---|
private Singleton()The only constructor, the private no-argument constructor,
can only be called from this class, i.e., within the factory method.
It should be called exactly once, i.e., the first time the static
factory is called.
if (singleton == null)
singleton = this;
else throw new IllegalArgumentException(
"Default constructor called more than once.");
|
Methods Summary |
---|
static Singleton | getInstance()
if (singleton == null)
new Singleton();
return singleton;
| public static void | main(java.lang.String[] argv)
System.out.println("getInstance returns " + getInstance());
System.out.println("getInstance returns " + getInstance());
System.out.println("getInstance returns " + getInstance());
|
|