Singleton Class in Java

A singleton is a class that only ever has one single instance. For more information on the Singleton design pattern, please refer to the Singleton topic in the Design Patterns tag.

The program demonstrates the implementation of a Singleton class in Java. The Singleton pattern ensures that only one instance of a class can be created throughout the entire application lifecycle.

  • In this example, the ABC class is a Singleton class, with a private constructor and a static getInstance() method that returns the single instance of the class. The obj variable is used to store the single instance of the ABC class, which is created only if it is not already created.
  • The display() method is just a simple method that prints "I am Display" to the console.
  • In the main() method, we obtain the instance of the ABC class by calling the static getInstance() method. Then, we call the display() method on the object to demonstrate that only one instance of the ABC class is created and used throughout the entire application.

Source Code

//Singleton Class in Java
class ABC
    static ABC obj =null;
    private ABC(){}
    public static ABC getInstance()
            obj=new ABC();
        return obj;
    void display()
        System.out.println("I am Display");
public class Singleton {
    public static void main(String[] args) {
        ABC o=ABC.getInstance();


I am Display
To download raw file Click Here

Basic Programs