Singleton Pattern in Java
In this lesson: Create a singleton class in Java — eager, lazy and thread-safe versions — with dry run and output. In Hindi & English.
What is the Singleton pattern
The Singleton pattern makes sure a class has only one object for the entire program, and gives one clear way to access it. It is a classic design pattern used when exactly one shared instance makes sense — like a single database connection or a settings manager.
Why use it
- One shared resource — a single database connection, cache or logger.
- Consistent state — everyone uses the same object, so data stays in sync.
- Controlled access — creation happens in one place you control.
It fits any case where making several objects would be wasteful or wrong.
The Java program
class Database {
private static Database instance; // the single object
private Database() { // private constructor
System.out.println("Database created");
}
public static Database getInstance() {
if (instance == null) { // create only once
instance = new Database();
}
return instance; // always the same object
}
}
public class Main {
public static void main(String[] args) {
Database db1 = Database.getInstance();
Database db2 = Database.getInstance();
System.out.println(db1 == db2); // same object?
}
}
true
Notice "Database created" prints only once, and db1 == db2 is true — both variables point to the very same object.
Dry run
| Step | Call | instance == null? | What happens |
|---|---|---|---|
| 1 | getInstance() (db1) | yes (first time) | create new Database, store in instance |
| 2 | getInstance() (db2) | no (already exists) | return the same old instance |
| db1 and db2 are the same object → db1 == db2 is true | |||
The first getInstance() finds instance is null, so it creates the object. Every later call finds it already exists and returns the same one — so only one object ever exists.
The three key parts
- Private static field — holds the single instance.
- Private constructor — blocks others from using
new. - Public static getInstance() — creates it once, returns it always.
Together these three guarantee exactly one object, accessed the same way everywhere.
Thread safety note
The simple version above is not thread-safe: two threads calling getInstance() at the same time could both see instance == null and create two objects. For multithreaded code, make getInstance() synchronized, use an eager instance, or use an enum singleton.
Common mistakes
- Public constructor. Make it private, or others can create more objects.
- Forgetting the null check. Without it, a new object is made every call.
- Ignoring thread safety. Add synchronization for multithreaded use.
- Overusing Singletons. Too much global state makes code hard to test.
Summary
- The Singleton pattern allows only one object of a class.
- Use a private constructor, a static instance, and getInstance().
- getInstance() creates the object once and returns it every time.
- It suits shared resources like a connection or configuration.
- The basic version needs extra care to be thread-safe.
Next, revise objects in classes and objects, or the static keyword it relies on.
