Jump to Real's How-to Main page

Have a singleton

A singleton is a class that can be instantiated only one time in a JVM. Repeated calls always return the same instance.
public class OnlyOne{
    private static OnlyOne one = new OnlyOne();

    private OnlyOne(){}

    public static OnlyOne getInstance() { return one; }
} 
To use it
OnlyOne myOne = OnlyOne.getInstance();

If you find this article useful, consider making a small donation
to show your support for this Web site and its content.

Written and compiled by Réal Gagnon ©1998-2005
[ home ]