---
title: "How do you implement a singleton pattern in Java?"  
description: "How do you implement a singleton pattern in Java?"  
author: "Anubhav Sharma"  
published: 2024-07-18  
updated: 2024-07-18  
canonical: https://www.mindstick.com/forum/160945/how-do-you-implement-a-singleton-pattern-in-java  
category: "java"  
tags: ["java", "javac"]  
reading_time: 3 minutes  

---

# How do you implement a singleton pattern in Java?

How do you implement a [singleton pattern](https://www.mindstick.com/forum/158344/how-do-you-implement-a-singleton-pattern-in-javascript) in Java? Explain the different ways to create a [singleton class](https://www.mindstick.com/interview/2473/what-is-singleton-class).

## Replies

### Reply by Ravi Vishwakarma

The [Singleton](https://www.mindstick.com/blog/10912/java-singleton-pattern) [pattern](https://www.mindstick.com/forum/2314/what-is-the-mvp-and-mvc-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:

- 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.

```plaintext
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.

```plaintext
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.

```plaintext
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.

```plaintext
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.

```plaintext
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.
- **Enum Singleton:** Thread-safe, handles serialization, simple.

## Read more

[**Describe the life cycle of a thread in Java.**](https://www.mindstick.com/forum/160947/describe-the-life-cycle-of-a-thread-in-java)

[**Explain the difference between checked and unchecked exceptions. Provide examples.**](https://www.mindstick.com/forum/160948/explain-the-difference-between-checked-and-unchecked-exceptions-provide-examples)


---

Original Source: https://www.mindstick.com/forum/160945/how-do-you-implement-a-singleton-pattern-in-java

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
