FileDocCategorySizeDatePackage
Singleton.javaAPI DocExample1032Sat Nov 25 12:54:20 GMT 2000None

Singleton

public class Singleton extends Object
An example of a Singleton implementation in Java.
author
Ian F. Darwin, ian@darwinsys.com
version
$Id: Singleton.java,v 1.4 2000/11/25 17:54:20 ian Exp $

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 SingletongetInstance()

		if (singleton == null)
			new Singleton();
		return singleton;
	
public static voidmain(java.lang.String[] argv)

		System.out.println("getInstance returns " + getInstance());
		System.out.println("getInstance returns " + getInstance());
		System.out.println("getInstance returns " + getInstance());