The Singleton Class in Java is one of the most important and widely used design patterns in Java development. It ensures that only one instance of a class is created and provides a global access point to that instance.
This guide is designed for beginners and intermediate Java developers who want a clear, practical, and real-world understanding of the Java Singleton Pattern.
A Singleton Class restricts object creation so that only one instance exists throughout the application lifecycle.
To create a Singleton class in Java, you need to follow three important rules:
class Singleton { // Private static variable to hold the single instance private static Singleton instance; // Private constructor to prevent instantiation private Singleton() { } // Public method to provide access to the instance public static Singleton getInstance() { if (instance == null) { instance = new Singleton(); } return instance; } }
This is the simplest form of Singleton, also known as lazy initialization because the object is created only when it is needed.
class Singleton { private static Singleton instance; private Singleton() { } public static Singleton getInstance() { if (instance == null) { instance = new Singleton(); } return instance; } }
This implementation creates the object only when it is required, but it is not thread-safe.
class EagerSingleton { private static final EagerSingleton instance = new EagerSingleton(); private EagerSingleton() { } public static EagerSingleton getInstance() { return instance; } }
class ThreadSafeSingleton { private static ThreadSafeSingleton instance; private ThreadSafeSingleton() { } public static synchronized ThreadSafeSingleton getInstance() { if (instance == null) { instance = new ThreadSafeSingleton(); } return instance; } }
class DoubleCheckedSingleton { private static volatile DoubleCheckedSingleton instance; private DoubleCheckedSingleton() { } public static DoubleCheckedSingleton getInstance() { if (instance == null) { synchronized (DoubleCheckedSingleton.class) { if (instance == null) { instance = new DoubleCheckedSingleton(); } } } return instance; } }
enum SingletonEnum { INSTANCE; public void showMessage() { System.out.println("Enum Singleton Example"); } }
The Enum Singleton is thread-safe, prevents reflection attacks, and handles serialization automatically.
| Implementation | Thread Safe | Lazy | Performance |
|---|---|---|---|
| Eager Initialization | Yes | No | High |
| Lazy Initialization | No | Yes | High |
| Synchronized Method | Yes | Yes | Low |
| Enum Singleton | Yes | No | High |
A Singleton Class ensures that only one instance of a class is created and provides global access to it.
No, only certain implementations like Enum Singleton or synchronized methods are thread-safe.
It provides built-in thread safety, serialization handling, and protection against reflection.
Yes, improper implementation using reflection or serialization can break Singleton behavior.
Singleton is commonly used in logging, database connections, caching, configuration management, and thread pools.
The Singleton Class in Java is a powerful design pattern that helps manage shared resources efficiently. By understanding different implementations and best practices, developers can build scalable, maintainable, and high-performance Java applications.
Copyrights © 2024 letsupdateskills All rights reserved