🌐
GeeksforGeeks
geeksforgeeks.org › java › singleton-class-java
Singleton Method Design Pattern in Java - GeeksforGeeks
In object-oriented programming, a Java singleton class is a class that can have only one object (an instance of the class) at a time. After the first time, if we try to instantiate the Java Singleton classes, the new variable also points to ...
Published   July 23, 2025
design pattern in object-oriented software development
In object-oriented programming, the singleton pattern is a software design pattern that restricts the instantiation of a class to a singular instance. It is one of the well-known "Gang of Four" design … Wikipedia
Factsheet
Name Singleton
Factsheet
Name Singleton
🌐
Wikipedia
en.wikipedia.org › wiki › Singleton_pattern
Singleton pattern - Wikipedia
October 17, 2025 - In object-oriented programming, the singleton pattern is a software design pattern that restricts the instantiation of a class to a singular instance. It is one of the well-known "Gang of Four" design patterns, which describe how to solve recurring problems in object-oriented software.
Discussions

java - I dont understand the concept of a singleton class - Stack Overflow
I know how to make a singleton class in java but what I dont understand is the concept of singleton. Like why would I need a singleton class and why would I use a singleton instead of a regular cla... More on stackoverflow.com
🌐 stackoverflow.com
design patterns - why are there java singleton classes? When would you need to use one - Stack Overflow
I understand that a singleton class is one where there can be only one instantiation, but I don't understand why this would be useful. Why won't you just create a class with static variables and More on stackoverflow.com
🌐 stackoverflow.com
Singleton pattern in java..
A singleton pattern is used when you want to ensure that throughout the application, there is at most one instance of an object that is instanciated, and that instance is shared between all the processes that needs it. One example is for services that could consume too much resource if initialized and discarded every time they are needed. At my current place of work, we use the pattern for REST services in the back end part of the application. From wikipedia : In software engineering, the singleton pattern is a software design pattern that restricts the instantiation of a class to one. This is useful when exactly one object is needed to coordinate actions across the system. The term comes from the mathematical concept of a singleton. The linked article shows a class diagram for a singleton and some examples. A vary basic singleton class would be: public class Singleton { // If you couldn't tell from the variable name, this is the single instance of your singleton class private static Singleton singleInstance; // The constructor is private to ensure it cannot be called from another class, and then creating another (unwanted) instance private Singleton(){ super(); } // And this method returns your instance public static Singleton getInstance(){ // If the instance doesn't exist, I create it, otherwise I do nothing. if(singleInstance == null){ singleInstance = new Singleton(); // I can call the constructor here because I'm within the class } // Then I return the instance, whether I just created it, or it was preexisting) return singleInstance; } } This is the bare bones for lazy initialization, and doesn't take care of synchronization (which may or may not be needed) More on reddit.com
🌐 r/javahelp
19
1
January 3, 2019
java - What is a singleton, in plain English? - Stack Overflow
The simple plain English1 version is: Singleton Class is a Class that has, and can only have, only one instance. But can't you just then use a static class for that? No. That's not what a "static" class is in Java. In Java "static" classes can have multiple instances just like any other class. The static keyword is used (for classes) to mean ... More on stackoverflow.com
🌐 stackoverflow.com
People also ask

Can I use Enum for implementing Singleton?
You can use an Enum to implement the singleton pattern in Java. Enum values are inherently single instances by design, making them a straightforward and thread-safe way to create Singleton objects.  Here's an example of how you can achieve this:
🌐
simplilearn.com
simplilearn.com › home › resources › software development › java tutorial for beginners › java singleton class: benefits & implementation
Java Singleton Class: A Complete Guide
How does serialization affect Singleton?
Serialization can affect the behavior of Singleton classes in Java. When a Singleton class is serialized and then deserialized, it can create multiple instances, violating the Singleton pattern's goal of having only one instance.
🌐
simplilearn.com
simplilearn.com › home › resources › software development › java tutorial for beginners › java singleton class: benefits & implementation
Java Singleton Class: A Complete Guide
Can a Singleton class be subclassed?
Yes, it is possible. But it can lead to many complications. Each Subclass would have its instance. It will break the intended single-instance behavior. The Subclass should handle initialization and constructor access differently, potentially affecting the singleton pattern's purpose.
🌐
simplilearn.com
simplilearn.com › home › resources › software development › java tutorial for beginners › java singleton class: benefits & implementation
Java Singleton Class: A Complete Guide
🌐
Programiz
programiz.com › java-programming › singleton
Example: Java Singleton Class Syntax
In Java, Singleton is a design pattern that ensures that a class can only have one object.
🌐
DigitalOcean
digitalocean.com › community › tutorials › java-singleton-design-pattern-best-practices-examples
Java Singleton Pattern: Best Practices & Examples | DigitalOcean
November 5, 2022 - To overcome this situation with Reflection, Joshua Bloch suggests the use of enum to implement the singleton design pattern as Java ensures that any enum value is instantiated only once in a Java program. Since Java Enum values are globally accessible, so is the singleton.
🌐
Refactoring.Guru
refactoring.guru › home › design patterns › singleton › java
Singleton in Java / Design Patterns
January 1, 2026 - Full code example in Java with detailed comments and explanation. Singleton is a creational design pattern, which ensures that only one object of its kind exists and provides a single point of access to it for any other code.
🌐
Oracle
oracle.com › java › technical details
When is a Singleton not a Singleton?
The Singleton should not be seen as way to implement global variables in the Java programming language; rather, along the lines of the factory design patterns, the Singleton lets you encapsulate and control the creation process by making sure that certain prerequisites are fulfilled or by creating the object lazily on demand. However, in certain situations, two or more Singletons can mysteriously materialize, disrupting the very guarantees that the Singleton is meant to provide.
Find elsewhere
🌐
Baeldung
baeldung.com › home › java › core java › singletons in java
Singletons in Java | Baeldung
October 23, 2025 - If we need a singleton, we might consider the possibility of delegating its instantiation to another class, a sort of factory, that should take care of assuring that there’s just one instance of the singleton in play.
🌐
Tutorialspoint
tutorialspoint.com › java › java_singleton_class.htm
Java - Singleton Class
Singleton pattern is one of the simplest design patterns in Java. This type of design pattern comes under creational pattern as this pattern provides one of the best ways to create an object.
Top answer
1 of 3
2

Singletons are used for when you want exactly one instance of a class, that the entire application shares.

Good examples for this principle are classes that are in charge of accessing external resources. For example, you'd want the entire application share the same database connection (or at least connection pool), not have every class that needs it open its own connection.

2 of 3
0

In some cases, we need to expose a shared resource throughout the application e.g. DB connection but we don't want to

  1. create shared object up-front (before creation of client objects).
  2. explicitly pass shared object to each client object.

then we can use Singleton design pattern.

Typical Singleton class looks like

public class MySingleton {
      private MySingleton INSTANCE
      private MySingleton() {
      }

      public static MySingleton getInstance() {
       if (INSTANCE == null) {
         syncronized (MySingleton.class) {
           if (INSTANCE == null) {
             INSTANCE = new MySingleton();
           }
         }
       }
       return INSTANCE;
      }
     // instance methods exposing business operation
    }

But we can achieve the similar behaviour by making each and every instance methods which are exposing business operation as static. In this approach we don't even need to create single object.
Then why do we need Singleton?
Well, the answer is simple. To isolate actual implementation from the client. Basically we are applying abstraction OOP principle here.
This is helpful If the singleton class is part of library which is used by various clients and library wants to vary the implementation as per the client.

Sample for such singleton can be

public class MySingleton {
      private MySingleton INSTANCE
      private MySingleton() {
      }

      public static MySingleton getInstance() {
       if (INSTANCE == null) {
         syncronized (MySingleton.class) {
           if (INSTANCE == null) {
             INSTANCE = new MySingletonChild(); // here we are creating an object of subclass of MySingleton.
           }
         }
       }
       return INSTANCE;
      }
     // instance methods exposing business operation
    }

Hope this help in understanding Singleton design pattern.

🌐
Simplilearn
simplilearn.com › home › resources › software development › java tutorial for beginners › java singleton class: benefits & implementation
Java Singleton Class: A Complete Guide
July 31, 2025 - A Java Singleton class ensures only one instance of a class at a time. Learn how to implement this design pattern in object-oriented programming.
Address   5851 Legacy Circle, 6th Floor, Plano, TX 75024 United States
🌐
Refactoring.Guru
refactoring.guru › home › design patterns › creational patterns
Singleton
January 1, 2026 - Singleton is a creational design pattern that lets you ensure that a class has only one instance, while providing a global access point to this instance.
Top answer
1 of 11
14

While I agree with the other answers, the OP was asking why not have a class with all static methods (possibly with static fields) instead of a singleton where you have one instance.

Why use Singletons?

You can Google "singleton" to find all sorts of reasons. From JavaWorld:

Sometimes it's appropriate to have exactly one instance of a class: window managers, print spoolers, and filesystems are prototypical examples. Typically, those types of objects—known as singletons—are accessed by disparate objects throughout a software system, and therefore require a global point of access. Of course, just when you're certain you will never need more than one instance, it's a good bet you'll change your mind.

Why use a Singleton instead of a class with all static methods?

A few reasons

  1. You could use inheritance
  2. You can use interfaces
  3. It makes it easier to do unit testing of the singleton class itself
  4. It makes it possible to do unit testing of code that depends on the singleton

For #3, if your Singleton was a database connection pool, you want to insure that your application has only one instance, but do unit testing of the database connection pool itself without hitting the database (possibly by using a package-scope constructor or static creational method):

public class DatabaseConnectionPool {
  private static class SingletonHolder {
    public static DatabaseConnectionPool instance = new DatabaseConnectionPool(
        new MySqlStatementSupplier());
  }

  private final Supplier<Statement> statementSupplier;

  private DatabaseConnectionPool(Supplier<Statement> statementSupplier) {
    this.statementSupplier = statementSupplier;
  }

  /* Visibile for testing */
  static DatabaseConnectionPool createInstanceForTest(Supplier<Statement> s) {
    return new DatabaseConnectionPool(s);
  }

  public static DatabaseConnectionPool getInstance() {
    return SingletonHolder.instance;
  }

  // more code here
}

(notice the use of the Initialization On Demand Holder pattern)

You can then do testing of the DatabaseConnectionPool by using the package-scope createInstanceForTest method.

Note, however, that having a static getInstance() method can cause "static cling", where code that depends on your singleton cannot be unit tested. Static singletons are often not considered a good practice because of this (see this blog post)

Instead, you could use a dependency injection framework like Spring or Guice to insure that your class has only one instance in production, while still allowing code that uses the class to be testable. Since the methods in the Singleton aren't static, you could use a mocking framework like JMock to mock your singleton in tests.

2 of 11
4

A class with only static methods (and a private contructor) is a variant where there is no instance at all (0 instances).

A singleton is a class for which there is exactly 1 instance.

Those are different things and have different use cases. The most important thing is state. A singleton typically guards access to something of which there is logically only ever one. For instance, -the- screen in an application might be represented by a singleton. When the singleton is created, resources and connections to this one thing are initialized.

This is a big difference with a utility class with static methods - there is no state involved there. If there was, you would have to check (in a synchronized block) if the state was already created and then initialize it on demand (lazily). For some problems this is indeed a solution, but you pay for it in terms of overhead for each method call.

🌐
Tutorialspoint
tutorialspoint.com › java › java_using_singleton.htm
Java - How to Use Singleton Class?
February 24, 2020 - Singleton pattern is one of the simplest design patterns in Java. This type of design pattern comes under creational pattern as this pattern provides one of the best ways to create an object.
🌐
Quora
quora.com › What-does-Singleton-mean-in-Java
What does Singleton mean in Java? - Quora
According to this pattern ,”define a class that has only one instance and provides a global point of access to it”. In other words, a class must ensure that only single instance should be created and single object can be used by all ...
🌐
Reddit
reddit.com › r/javahelp › singleton pattern in java..
r/javahelp on Reddit: Singleton pattern in java..
January 3, 2019 -

What is singleton pattern in java..? what advantage of use this.? why singleton pattern is used??. please explain me with real life example..

Top answer
1 of 2
3
A singleton pattern is used when you want to ensure that throughout the application, there is at most one instance of an object that is instanciated, and that instance is shared between all the processes that needs it. One example is for services that could consume too much resource if initialized and discarded every time they are needed. At my current place of work, we use the pattern for REST services in the back end part of the application. From wikipedia : In software engineering, the singleton pattern is a software design pattern that restricts the instantiation of a class to one. This is useful when exactly one object is needed to coordinate actions across the system. The term comes from the mathematical concept of a singleton. The linked article shows a class diagram for a singleton and some examples. A vary basic singleton class would be: public class Singleton { // If you couldn't tell from the variable name, this is the single instance of your singleton class private static Singleton singleInstance; // The constructor is private to ensure it cannot be called from another class, and then creating another (unwanted) instance private Singleton(){ super(); } // And this method returns your instance public static Singleton getInstance(){ // If the instance doesn't exist, I create it, otherwise I do nothing. if(singleInstance == null){ singleInstance = new Singleton(); // I can call the constructor here because I'm within the class } // Then I return the instance, whether I just created it, or it was preexisting) return singleInstance; } } This is the bare bones for lazy initialization, and doesn't take care of synchronization (which may or may not be needed)
2 of 2
-2
When you're thinking about using a singleton, consider whether using static variables and methods would make more sense. Many people don't like the singleton pattern because it's used too often. Consider something like this instead: public class NotASingleton { private static String myProperty; static { //load myProperty from file, for example. myProperty = .... } public static String getMyProperty(){ return myProperty; } } Now you can use that more easily than a singleton. System.out.println(NotASingleton.getMyProperty()); Instead of like this for the singleton: System.out.println(MySingleton.getInstance().getMyProperty()); It might make sense to use a singleton when initializing your class is very expensive, you may not need to initialize it to begin with, and at most you'll only need one instance. If you will always need one instance you may as well just initialize static variables and use static methods.
Top answer
1 of 3
14

The simple plain English1 version is: Singleton Class is a Class that has, and can only have, only one instance.

But can't you just then use a static class for that?

No. That's not what a "static" class is in Java. In Java "static" classes can have multiple instances just like any other class.

The static keyword is used (for classes) to mean that the instance of a nested class is not tied to a specific instance of the enclosing class. And that means that expressions in the nested class cannot refer to instance variables declared in the enclosing class.

Prior to Java 1.5 (aka Java 5), there was no support for the singleton design pattern in Java. You just implemented them in plain Java; e.g.

    /** There is only one singer and he knows only one song */
    public class Singer {
        private static Singer theSinger = new Singer();
        private String song = "I'm just a singer";

        private Singer() { 
            /* to prevent instantiation */
        }

        public static Singer getSinger() { 
            return theSinger; 
        }

        public String getSong() {
            return song;
        }
    }

Java 1.5 introduced the enum types which can be used to implement singletons, etc.

    /** There are two Singers ... no more and no less */
    public enum Singer {
        DUANE("This is my song"),
        RUPERT("I am a singing bear");
        
        private String song;

        Singer(String song) {
            this.song = song;
        }
        
        public String getSong() {
            return song;
        }
    }

1 - Of course, you need to understand what "class" and "instance" mean. Since the Programming / IT English meanings of these words is different to the "plain English" meanings, it is a stretch to call this a "plain English" description. On the other hand, if the reader doesn't already understand what "class" and "instance" mean, they don't have the base knowledge needed to understand the "singleton" idea, or see the point of it.

2 of 3
1

singleton is a class with a private constructor and you could only get one instance of it. for further explanation why this coding style is done I suggest you read the chapter regarding singletons in this book

http://www.wowebook.com/book/head-first-design-patterns/

Chapter 5 is all about singleton

🌐
JavaTechOnline
javatechonline.com › home › singleton class in java with examples: a comprehensive guide
Singleton Class In Java With Examples: A Comprehensive Guide - JavaTechOnline
August 31, 2025 - The main objective of creating a Singleton class in Java is to restrict the creation of number of objects. Moreover, By using the Singleton class, the object would not be created each time a new request is made. Instead, a single object will be used repeatedly.
🌐
GeeksforGeeks
geeksforgeeks.org › system design › singleton-design-pattern
Singleton Method Design Pattern - GeeksforGeeks
It provides a thread-safe and efficient way to create the singleton instance. In Java, a Singleton can be implemented using a static inner class.
Published   February 11, 2026
🌐
DEV Community
dev.to › adityapratapbh1 › the-singleton-design-pattern-ensuring-a-single-instance-in-java-5c1o
The Singleton Design Pattern: Ensuring a Single Instance in Java - DEV Community
October 24, 2023 - The Singleton design pattern is one of the most basic and is often used in Java and other object-oriented programming languages to construct a single instance of a class that is shared across the application.
🌐
Quora
quora.com › What-is-Singleton-class-and-how-to-use-it-in-Java
What is Singleton class and how to use it in Java? - Quora
Answer (1 of 5): “Singleton” is a pattern, not a class. A given class may be a singleton - a class that is designed to allow only one instantiation. It’s similar to a static class in that it creates a single point of invocation.