Java: Single Class β Real Use Cases & Interview Q&A
π¨βπΌ Interviewer: Can you explain what a Singleton class is?
π¨βπ» Candidate:
Sure. A singleton class is a special class where only one object (instance) is ever created in the entire application. It’s mainly used when we want to share the same object across different parts of the system β for example, a logging service or a configuration manager.
π¨βπΌ Interviewer: Okay, where have you used Singleton in your projects?
π¨βπ» Candidate:
Iβve used Singleton in places like:
- Central logging utility
- Application-wide configuration loader
- Database connection pool manager (though we usually use frameworks like HikariCP now)
- Cache manager (like a wrapper over Redis or in-memory cache)
π¨βπΌ Interviewer: How do you create a singleton class in Java?
π¨βπ» Candidate:
There are a few ways, but the most common is eager initialization:
public class MySingleton {
private static final MySingleton INSTANCE = new MySingleton();
private MySingleton() {}
public static MySingleton getInstance() {
return INSTANCE;
}
}
This is thread-safe and simple because the instance is created at class loading time.
π¨βπΌ Interviewer: Is this implementation thread-safe?
π¨βπ» Candidate:
Yes, the eager initialization version is thread-safe by default. If we need lazy initialization, then we have to be careful with synchronization.
π¨βπΌ Interviewer: What if two threads call getInstance()
at the same time?
π¨βπ» Candidate:
In the eager initialization approach β itβs safe. But in lazy initialization, we should use the Bill Pugh Singleton pattern which uses a static inner class:
public class MySingleton {
private MySingleton() {}
private static class Holder {
private static final MySingleton INSTANCE = new MySingleton();
}
public static MySingleton getInstance() {
return Holder.INSTANCE;
}
}
This approach is also thread-safe and supports lazy loading.
π¨βπΌ Interviewer: What happens if we try to clone a Singleton object?
π¨βπ» Candidate:
Good question. Cloning can break Singleton because it creates a new object. To prevent that, we override the clone()
method:
@Override
protected Object clone() throws CloneNotSupportedException {
throw new CloneNotSupportedException("Cloning not allowed for Singleton");
}
π¨βπΌ Interviewer: Can serialization also break Singleton?
π¨βπ» Candidate:
Yes. During deserialization, a new object is created. To fix this, we use readResolve()
:
private Object readResolve() {
return getInstance();
}
This ensures that deserialization returns the same singleton instance.
π¨βπΌ Interviewer: Nice. Whatβs the safest and simplest way to implement Singleton in Java?
π¨βπ» Candidate:
Using an enum
. It solves all issues β thread safety, serialization, even reflection:
public enum MySingleton {
INSTANCE;
public void doSomething() {
// business logic
}
}
π¨βπΌ Interviewer: Why not just use static methods then?
π¨βπ» Candidate:
Good point. Static methods are okay for utility-like operations. But Singleton gives us flexibility:
- We can implement interfaces
- We can lazy-load
- We can test or mock the singleton object
- We can maintain internal state
Static methods can’t do that.
π¨βπΌ Interviewer: How do you test Singleton in unit tests?
π¨βπ» Candidate:
I usually check that two getInstance()
calls return the same object using ==
.
If needed, I use reflection to reset the singleton (but only in test cases β not recommended in prod).
π¨βπΌ Interviewer: Final question β what mistakes should we avoid with Singleton?
π¨βπ» Candidate:
- Avoid using lazy initialization without synchronization
- Donβt allow cloning or serialization without protection
- Be careful of using Singleton for things that need to scale horizontally (like microservices)
- Donβt use Singleton for everything β itβs a design tool, not a rule
π Summary
Singleton class is powerful but needs careful design. In interviews, donβt just show code β explain why and where itβs useful. Discuss thread safety, cloning, serialization, and real scenarios.
Leave a Reply