Explain the Singleton Design Pattern in Java

πŸ’‘ Concept: Singleton Pattern

The Singleton pattern ensures that a class has only one instance and provides a global point of access to it.

πŸ“˜ Quick Intro

Singleton pattern restricts object creation of a class to one "single" instance for system-wide use.

🧠 Analogy

Think of a Singleton like the government β€” there’s only one central government (object) everyone refers to. Multiple governments (instances) would create chaos.

πŸ”§ Technical Explanation

  • Uses a private static variable to hold the single instance.
  • Constructor is private to restrict instantiation.
  • A public static method provides the global access point.
  • Thread-safe implementations use synchronization or Bill Pugh pattern.
  • Singleton can be eager, lazy, or double-checked.

🎯 Use Cases

  • βœ… Configuration managers
  • βœ… Logger instances
  • βœ… Database connections
  • βœ… Cache objects

πŸ’» Example: Thread-safe Singleton


public class Singleton {
    private static volatile Singleton instance;

    private Singleton() {}

    public static Singleton getInstance() {
        if (instance == null) {
            synchronized(Singleton.class) {
                if (instance == null) {
                    instance = new Singleton();
                }
            }
        }
        return instance;
    }
}

❓ Interview Q&A

Q1: What is Singleton pattern?
A: A pattern that allows only one instance of a class.

Q2: How is Singleton implemented?
A: Using a private constructor and static instance.

Q3: What is double-checked locking?
A: Checks instance twice with synchronization for efficiency.

Q4: What are the types of Singleton?
A: Eager, Lazy, Bill Pugh, Enum.

Q5: Can Singleton be broken?
A: Yes, via reflection, cloning, or serialization.

Q6: How to make Singleton serialization-safe?
A: Override readResolve() method.

Q7: Why is Singleton considered an anti-pattern sometimes?
A: Because it introduces global state and tight coupling.

Q8: Is Enum Singleton thread-safe?
A: Yes, by default.

Q9: Can Spring beans be Singleton?
A: Yes, default scope is Singleton.

Q10: What's the Bill Pugh Singleton implementation?
A: Uses inner static helper class to hold instance.

πŸ“ MCQs

Q1. What is the purpose of Singleton pattern?

  • Provide multiple objects
  • Hide constructor
  • Ensure only one instance of a class exists
  • Enable inheritance

Q2. How is Singleton object accessed?

  • With new keyword
  • Via constructor
  • Using a static method
  • Via factory method

Q3. Which is thread-safe Singleton implementation?

  • Lazy loading
  • Eager loading
  • Double-checked locking
  • Cloning

Q4. What is the role of readResolve()?

  • Reset object
  • Log serialization
  • Prevent breaking Singleton during deserialization
  • None

Q5. Which is easiest way to create Singleton in Java?

  • Static block
  • Bill Pugh
  • Using Enum
  • Reflection

Q6. Why make constructor private?

  • Improve speed
  • Prevent external instantiation
  • Allow inheritance
  • Enable serialization

Q7. What design problem does Singleton address?

  • Multiple inheritance
  • Observer management
  • Global access to a single instance
  • Object pooling

Q8. Can Singleton be used in multithreading?

  • No
  • Yes, always safe
  • Yes, with proper synchronization
  • Only in Java 11+

Q9. What is common use case of Singleton?

  • Servlet
  • Logger
  • View
  • Controller

Q10. Which keyword is used in Bill Pugh Singleton?

  • abstract
  • final
  • static inner class
  • super

πŸ’‘ Bonus Insight

The Enum-based Singleton is the simplest and safest Singleton implementation recommended since Java 1.5.

πŸ“„ PDF Download

Need a handy summary for your notes? Download this topic as a PDF!

πŸ” Navigation

Learn More About Java β˜•

What is Java and its key features πŸ‘‰ Explained
Explain the Java Virtual Machine (JVM) πŸ‘‰ Explained
Difference between JDK, JRE, and JVM πŸ‘‰ Explained
What are Java’s main data types πŸ‘‰ Explained
Explain the concept of Object-Oriented Programming in Java πŸ‘‰ Explained
What is the difference between a class and an object πŸ‘‰ Explained
Explain encapsulation with an example πŸ‘‰ Explained
What is inheritance in Java and its types πŸ‘‰ Explained
Define polymorphism in Java with examples πŸ‘‰ Explained
What is abstraction in Java πŸ‘‰ Explained
Difference between abstract class and interface in Java πŸ‘‰ Explained
Explain method overloading and method overriding in Java πŸ‘‰ Explained
What are constructors in Java πŸ‘‰ Explained
What is the use of the static keyword in Java πŸ‘‰ Explained
Explain the difference between final, finally, and finalize in Java πŸ‘‰ Explained
Share:

Tags:


Feedback Modal Popup