In Java Singleton class or design pattern restricts the instantiation of a class to a singular instance.
Following example shows a classic Singleton design pattern implementation:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
public class ClassicSingleton { private static ClassicSingleton instance = null; protected ClassicSingleton() { // Exists only to defeat instantiation. } public static ClassicSingleton getInstance() { if (instance == null) { instance = new ClassicSingleton(); } return instance; } } |