public class Singleton {
// Step 1: Create a private static variable to hold the single instance of the class
private static Singleton instance;
// Step 2: Make the constructor private so it cannot be instantiated outside
private Singleton() {
// private constructor to prevent instantiation
}
// Step 3: Provide a public static method to get the instance of the class
public static Singleton getInstance() {
if (instance == null) {
instance = new Singleton(); // Create instance only if it doesn't exist
}
return instance; // Return the single instance
}
}