⬅ Previous Topic
Java Anonymous ClassNext Topic ⮕
Java Enums⬅ Previous Topic
Java Anonymous ClassNext Topic ⮕
Java EnumsIn Java, a Singleton class is a class that allows only one instance of itself to be created—and gives access to that single instance. It’s a widely used design pattern, especially when you need exactly one object to coordinate actions across the system.
Imagine you're logging actions throughout your application. Would you want multiple loggers writing randomly? Of course not. A singleton logger ensures consistent behavior and state—no matter where it’s used.
Let’s go through different ways to implement a singleton class. Each method has its own nuances, especially in multi-threaded environments.
The instance is created at the time of class loading.
public class EagerSingleton {
private static final EagerSingleton instance = new EagerSingleton();
private EagerSingleton() {
System.out.println("Instance created");
}
public static EagerSingleton getInstance() {
return instance;
}
}
public class Main {
public static void main(String[] args) {
EagerSingleton obj1 = EagerSingleton.getInstance();
EagerSingleton obj2 = EagerSingleton.getInstance();
System.out.println(obj1 == obj2); // true
}
}
Instance created
true
Only one object is created. Even if we try to get the instance multiple times, we get the same reference.
In this approach, the instance is created only when it’s needed.
public class LazySingleton {
private static LazySingleton instance;
private LazySingleton() {}
public static LazySingleton getInstance() {
if (instance == null) {
instance = new LazySingleton();
System.out.println("Instance created");
}
return instance;
}
}
public class Main {
public static void main(String[] args) {
LazySingleton obj1 = LazySingleton.getInstance();
LazySingleton obj2 = LazySingleton.getInstance();
System.out.println(obj1 == obj2); // true
}
}
Instance created
true
The object is created only when getInstance()
is called the first time. It saves resources but is not thread-safe.
This method ensures thread safety using synchronization.
public class ThreadSafeSingleton {
private static ThreadSafeSingleton instance;
private ThreadSafeSingleton() {}
public static synchronized ThreadSafeSingleton getInstance() {
if (instance == null) {
instance = new ThreadSafeSingleton();
System.out.println("Instance created");
}
return instance;
}
}
The synchronized
keyword ensures that only one thread can execute this method at a time. It's safe, but might reduce performance due to synchronization overhead.
This is a more efficient way of making the singleton thread-safe.
public 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();
System.out.println("Instance created");
}
}
}
return instance;
}
}
Using Enum is the simplest and most effective way to implement a singleton in Java. It prevents reflection and serialization issues.
public enum EnumSingleton {
INSTANCE;
public void show() {
System.out.println("Singleton using Enum");
}
}
public class Main {
public static void main(String[] args) {
EnumSingleton singleton = EnumSingleton.INSTANCE;
singleton.show();
}
}
Singleton using Enum
Singleton pattern is used across frameworks and applications:
⬅ Previous Topic
Java Anonymous ClassNext Topic ⮕
Java EnumsYou can support this website with a contribution of your choice.
When making a contribution, mention your name, and programguru.org in the message. Your name shall be displayed in the sponsors list.