The Singletonpattern ensures that a class has only one instance and provides a global point of access to it. Here are different ways to create a singleton class in Java:
1. Eager Initialization
In eager initialization, the instance of the Singleton class is created at the time of class loading.
Pros:
Simple to implement.
The instance is created even if it might not be used.
Cons:
May cause resource wastage if the instance is never used.
public class EagerSingleton {
private static final EagerSingleton instance = new EagerSingleton();
// Private constructor to prevent instantiation
private EagerSingleton() {}
public static EagerSingleton getInstance() {
return instance;
}
}
2. Lazy Initialization
In lazy initialization, the instance is created only when it is needed.
Pros:
The instance is created only when requested, saving resources.
Cons:
Not thread-safe.
public class LazySingleton {
private static LazySingleton instance;
// Private constructor to prevent instantiation
private LazySingleton() {}
public static LazySingleton getInstance() {
if (instance == null) {
instance = new LazySingleton();
}
return instance;
}
}
3. Thread-Safe Singleton (Synchronized Method)
This approach uses synchronization to make the getInstance method thread-safe.
Pros:
Thread-safe.
Cons:
Can be slow due to the synchronized method.
public class ThreadSafeSingleton {
private static ThreadSafeSingleton instance;
// Private constructor to prevent instantiation
private ThreadSafeSingleton() {}
public static synchronized ThreadSafeSingleton getInstance() {
if (instance == null) {
instance = new ThreadSafeSingleton();
}
return instance;
}
}
4. Double-Checked Locking
Double-checked locking reduces the overhead of acquiring a lock by first checking the condition without synchronization.
Pros:
Thread-safe.
High performance.
Cons:
Slightly more complex implementation.
public class DoubleCheckedLockingSingleton {
private static volatile DoubleCheckedLockingSingleton instance;
// Private constructor to prevent instantiation
private DoubleCheckedLockingSingleton() {}
public static DoubleCheckedLockingSingleton getInstance() {
if (instance == null) {
synchronized (DoubleCheckedLockingSingleton.class) {
if (instance == null) {
instance = new DoubleCheckedLockingSingleton();
}
}
}
return instance;
}
}
5. Bill Pugh Singleton (Static Inner Class)
This approach leverages the Java class loader mechanism to ensure that the instance is created only when the singleton class is loaded.
Pros:
Thread-safe.
Lazy initialization.
High performance.
public class BillPughSingleton {
private BillPughSingleton() {}
private static class SingletonHelper {
private static final BillPughSingleton INSTANCE = new BillPughSingleton();
}
public static BillPughSingleton getInstance() {
return SingletonHelper.INSTANCE;
}
}
Summary
Eager Initialization: Simple but may waste resources.
Lazy Initialization: Efficient but not thread-safe.
Thread-Safe Singleton (Synchronized Method): Thread-safe but slow.
Double-Checked Locking: Thread-safe and high performance.
Bill Pugh Singleton (Static Inner Class): Thread-safe, lazy initialization, high performance.
We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge that you have read and understood our
Cookie Policy &
Privacy Policy.
The Singleton pattern ensures that a class has only one instance and provides a global point of access to it. Here are different ways to create a singleton class in Java:
1. Eager Initialization
In eager initialization, the instance of the Singleton class is created at the time of class loading.
Pros:
Cons:
2. Lazy Initialization
In lazy initialization, the instance is created only when it is needed.
Pros:
Cons:
3. Thread-Safe Singleton (Synchronized Method)
This approach uses synchronization to make the
getInstancemethod thread-safe.Pros:
Cons:
4. Double-Checked Locking
Double-checked locking reduces the overhead of acquiring a lock by first checking the condition without synchronization.
Pros:
Cons:
5. Bill Pugh Singleton (Static Inner Class)
This approach leverages the Java class loader mechanism to ensure that the instance is created only when the singleton class is loaded.
Pros:
Summary
Read more
Describe the life cycle of a thread in Java.
Explain the difference between checked and unchecked exceptions. Provide examples.