Learn Java
import com.learnjava.oopadvanced.Singleton;

The Singleton Pattern in Java

@Concept

The Singleton pattern ensures a class has exactly one instance for the entire lifetime of a program, and provides one well-known, global way to access that single instance. The standard implementation makes the constructor private, so no other code can call new directly, stores the one allowed instance in a private static field, and exposes a public static method, conventionally named getInstance(), that creates that single instance on first call and returns the same one on every call after.

In a multithreaded program, a naive Singleton implementation can accidentally create two instances if two threads call getInstance() for the very first time simultaneously. That's why real-world implementations typically add synchronization, or simply initialize the instance eagerly at class-loading time instead of lazily on first use, sidestepping the race condition entirely.

@HowItWorks
1

A private constructor

Prevents any code outside the class from creating additional instances with new directly.

2

A private static field

Holds the one and only instance that will ever exist, once created.

3

A public static access method

Conventionally getInstance(), creates the single instance on first call and returns that same one on every later call.

4

Lazy versus eager initialization

Creating the instance only when getInstance() is first called (lazy) versus creating it immediately when the class loads (eager, which also avoids a threading race condition).

5

Why it matters

Genuinely shared, singular resources, like one central configuration object or one connection manager, should really only exist once.

@Example
public class Singleton {
private static Singleton instance;
 
private Singleton() {}
 
public static Singleton getInstance() {
if (instance == null) {
instance = new Singleton();
}
return instance;
}
 
public static void main(String[] args) {
Singleton a = Singleton.getInstance();
Singleton b = Singleton.getInstance();
System.out.println("Same instance: " + (a == b));
}
}
Output
Same instance: true
1The constructor is private, so Singleton objects can only ever be created from inside the class itself.
2The first call to getInstance() finds instance still null, so it creates the one and only Singleton object right there.
3The second call to getInstance() finds instance already set, and simply returns that same existing object instead of creating another.
4a == b compares references directly and confirms both variables point to the exact same single object.
@UseCases
Ensuring exactly one shared configuration object exists throughout an application's lifetime
Managing a single, centralized connection pool or resource manager
Providing one well-known global access point to a shared piece of state
Understanding why a naive Singleton needs extra care to stay safe across multiple threads

Recommended resourcesAffiliate links · may earn us a commission

Recommended for learners

Structured coding coursesVideo courses that pair well with visual practice — pick one language and go deep.Browse courses Pro code editor & AI assistantWrite, debug and explain code faster while you practice the patterns here.Try it free Classic DSA interview booksThe reference books interviewers themselves learned from — great next to the visualizers.See the books

Practice what you learned

Re-implement each visualized algorithm in Java yourself — building beats watching.