What is a Singleton Class in Java?

What is a Singleton Class in Java?

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.