🌐
GeeksforGeeks
geeksforgeeks.org › system design › singleton-design-pattern
Singleton Method Design Pattern - GeeksforGeeks
In Java, a Singleton can be implemented using a static inner class. A class is loaded into memory only once by the JVM. An inner class is loaded only when it is referenced. Therefore, the Singleton instance is created lazily, only when the ...
Published   February 11, 2026
🌐
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.
Discussions

java - example for Singleton pattern - Stack Overflow
Please give me a real time example for singleton pattern . Different threads accessing a shared file is singleton or not ? Since each thread access the same instance of the file not individual instances of their own . ... A "file" isn't a Java object (and java.io.File definitely isn't a singleton). 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
Singleton Class In Java With Examples: A Comprehensive Guide
The 'singleton' that is described here is actually an anti-pattern. You're basically creating global state that dependends rely on directly, making code hard to test. More on reddit.com
🌐 r/programming
3
0
February 14, 2024
What is an example of the singleton design pattern?
Logging class is the stereotypical example. All your classes pass log strings to one place and it handles writing to files or whatever you do to log stuff. More on reddit.com
🌐 r/learnprogramming
8
1
December 6, 2021
🌐
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.
🌐
Medium
medium.com › @pratik.941 › understanding-the-singleton-design-pattern-in-java-a-beginners-guide-79d63a67a41a
Understanding the Singleton Design Pattern in Java: A Beginner’s Guide | by Pratik T | Medium
July 12, 2024 - In Java programming, the Singleton Design Pattern is a widely used pattern that ensures a class has only one instance and provides a global point of access to that instance. This pattern is particularly useful when exactly one object is needed ...
🌐
Baeldung
baeldung.com › home › java › core java › singletons in java
Singletons in Java | Baeldung
October 23, 2025 - As for the EnumSingleton, we can use it like any other Java Enum: EnumSingleton enumSingleton1 = EnumSingleton.INSTANCE.getInstance(); System.out.println(enumSingleton1.getInfo()); //Initial enum info EnumSingleton enumSingleton2 = EnumSingleton.INSTANCE.getInstance(); enumSingleton2.setInfo("New enum info"); System.out.println(enumSingleton1.getInfo()); // New enum info System.out.println(enumSingleton2.getInfo()); // New enum info · Singleton is a deceptively simple design pattern, and there are a few common mistakes that a programmer might commit when creating a singleton.
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 - The following Java 5+ example is a thread-safe implementation, using lazy initialization with double-checked locking. public class Singleton { private static volatile Singleton instance = null; private Singleton() {} public static Singleton getInstance() { if (instance == null) { synchronized (Singleton.class) { if (instance == null) { instance = new Singleton(); } } } return instance; } } Some consider the singleton to be an anti-pattern that introduces global state into an application, often unnecessarily.
Find elsewhere
🌐
GeeksforGeeks
geeksforgeeks.org › java › singleton-class-java
Singleton Method Design Pattern in Java - GeeksforGeeks
To instantiate a normal class, we use a Java constructor. On the other hand, to instantiate a singleton class, we use the getInstance() method. The other difference is that a normal class vanishes at the end of the lifecycle of the application while the singleton class does not destroy with the completion of an application. There are two forms of singleton design patterns, which are:
Published   July 23, 2025
🌐
Refactoring.Guru
refactoring.guru › home › design patterns › creational patterns
Singleton
January 1, 2026 - Under the hood, this method calls the private constructor to create an object and saves it in a static field. All following calls to this method return the cached object. If your code has access to the Singleton class, then it’s able to call the Singleton’s static method. So whenever that method is called, the same object is always returned. The government is an excellent example of the Singleton pattern.
🌐
TutorialsPoint
tutorialspoint.com › design_pattern › singleton_pattern.htm
Design Patterns - Singleton Pattern
Create a Singleton Class. package com.tutorialspoint; public class SingleObject { //create an object of SingleObject private static SingleObject instance = new SingleObject(); //make the constructor private so that this class cannot be //instantiated private SingleObject(){} //Get the only object available public static SingleObject getInstance(){ return instance; } public void showMessage(){ System.out.println("Hello World!"); } }
🌐
Coderanch
coderanch.com › t › 100346 › engineering › Java-Singleton-Pattern-Tutorial
Java Singleton Pattern Tutorial (OO, Patterns, UML and Refactoring forum at Coderanch)
This design pattern proposes that at any time there can only be one instance of a singleton (object) created by the JVM. The Singleton class�s default constructor is made private, which prevents the direct instantiation of the object by others (Other Classes).
🌐
JavaTechOnline
javatechonline.com › home › singleton design pattern in java with all scenarios
Singleton Design Pattern In Java With All Scenarios Examples
November 15, 2025 - The Singleton Pattern puts restriction on the instantiation of a class and ensures that only one instance of the class exists in the java virtual machine. The singleton class must provide a global access point to get the instance of the class.
Top answer
1 of 7
4

A "file" isn't a Java object (and java.io.File definitely isn't a singleton). I wouldn't think of files on disk as a singleton either - they're just shared resources. In particular, it's not like there's only one file on disk :)

A more common example of the singleton pattern is configuration - or logging. For example, LogManager.getLogManager returns "the" LogManager, and you can't create new ones. Likewise you might have one common configuration object which can be accessed statically. (In a dependency injected system the configuration may well not be a singleton, however - instead each component is provided the bits of configuration they need so that they don't have to fetch a "common" one.)

2 of 7
1

Yes, but only if all threads access the same file, and you are using a custom implementation (not java.io.File, perhaps a wrapper)

the singleton pattern is a design pattern that is used to restrict instantiation of a class to one object

Singletons (being often a bad choice) are classes that can have only one instance throughout the whole program.

For example a SingletonConfigFile might look like this. Have in mind that:

  • it is for reading one file only. It would make sense for this to be a config file.
  • if your class can be instantiated more than once, for different files, it is not singleton.
  • don't use this code - it doesn't take into account concurrency problems which are a whole different area of discussion.

.

public SingletonConfigFile {
   private static String filename = "config.xml";
   private File file;
   private static SingletonConfigFile instance;

   private SingletonConfigFile() {
       if (instance != null) {
           throw new Error();
       }
       file = new File(filename);
   }

   public synchronized SingletonConfigFile getInstance() {
      if (instance == null) {
          instance = new SignletonConfigFile();
      }
      return instance
   }

   public String read() {
       // delegate to the underlying java.io.File
   }
}

But this example is on the edge of sense. Singletons are used in cases when there is only one object (as I stated above). For example it would make sense to have:

  • RingOfPower.getInstance() - there is only one ring of power (Sauron's), and there can't exist more.
  • Sun.getInstance() - only one star called "sun".
  • all objects in the withing of your application that logically should exist only once - a registry, an application context, etc.
🌐
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.
🌐
GeeksforGeeks
geeksforgeeks.org › java › java-singleton-design-pattern-practices-examples
Java Singleton Design Pattern Practices with Examples - GeeksforGeeks
January 3, 2025 - All these ways differ in their implementation of the pattern, but in the end, they all achieve the same end result of a single instance. Eager initialization: This is the simplest method of creating a singleton class. In this, object of class is created when it is loaded to the memory by JVM.
🌐
DZone
dzone.com › coding › java › singleton: 6 ways to write and use in java programming
Singleton: 6 Ways To Write and Use in Java Programming
April 9, 2024 - In Java programming, object creation or instantiation of a class is done with "new" operator and with a public constructor declared in the class as below. ... Clazz() is the default public constructor called with "new" operator to create or instantiate an object for Clazz class and assigned to variable clazz, whose type is Clazz. While creating a singleton, we have to ensure only one single object is created or only one instantiation of a class takes place.
🌐
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 assures that a class has only one instance and gives a global point of access to that instance. It is extensively used to manage shared resources and centralise control inside an application in Java and other ...
🌐
Medium
medium.com › @thecodebean › singleton-design-pattern-implementation-in-java-1fba4ecc959f
Singleton Design Pattern: Implementation in Java
September 29, 2023 - public class Main { public static void main(String[] args) { // Get the Singleton instance Singleton singleton = Singleton.getInstance(); // Use the Singleton // ... } } The Singleton Design Pattern ensures that there’s only one instance of a class in your application, promoting resource efficiency and providing global accessibility.
🌐
Programiz
programiz.com › java-programming
Learn Java Programming
Java Nested and Inner Class · Java Nested Static Class · Java Anonymous Class · Java Singleton Class · Java enums · Java enum Constructor · Java enum Strings · Java Reflection · Java Package · Java Exception Handling · Java Exceptions · Java Exception Handling ·
🌐
IT Experts
itexpertsconsultant.wordpress.com › 2015 › 05 › 17 › what-is-singleton-pattern-and-its-uses
What is Singleton pattern and it’s uses ?
January 15, 2018 - Singleton is a creation Design pattern, it’s used to control the initialization of Object and ensure that only one instance of Object will be created in the Java Virtual Machine.In terms of practical use Singleton patterns are used in logging, ...
🌐
DZone
dzone.com › coding › java › how to use singleton design pattern in java
How to Use Singleton Design Pattern in Java
July 20, 2018 - Now, first of all, what is Singleton and why is it required? The singleton design pattern is used to restrict the instantiation of a class and ensures that only one instance of the class exists in the JVM.