🌐
Programiz
programiz.com › java-programming › singleton
Java Singleton (With Example)
private SingletonExample() - a private constructor that restricts creating objects outside of the class.
🌐
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.
🌐
GeeksforGeeks
geeksforgeeks.org › java › singleton-class-java
Singleton Method Design Pattern in Java - GeeksforGeeks
In the above Java code, we use the getInstance() method to instantiate the singleton class and then when we again call with new key word it return the same instance which is created only once. The single_instance is static. so it shared the shared the shared among all it's instances. That is why the object x, y and z having the same object. Then we print the hashcode of all the object which is same which means ...
Published   July 23, 2025
🌐
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.
🌐
Tutorialspoint
tutorialspoint.com › java › java_singleton_class.htm
Java - Singleton Class
Since there is only one Singleton instance, any instance fields of a Singleton will occur only once per class, just like static fields. Singletons often control access to resources, such as database connections or sockets. For example, if you have a license for only one connection for your database or your JDBC driver has trouble with multithreading, the Singleton makes sure that only one connection is made or that only one thread can access the connection at a time.
🌐
Baeldung
baeldung.com › home › java › core java › singletons in java
Singletons in Java | Baeldung
October 23, 2025 - For example, singletons are often used to encompass the application’s configuration data (i.e., connection to the repository). If they’re used as global objects, it becomes difficult to choose the configuration for the test environment. Therefore, when we run the tests, the production database gets spoiled with the test data, which is hardly acceptable.
🌐
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 - Bill Pugh’s Singleton, also known as the Initialization on Demand Holder (IODH), is a thread-safe and efficient way to implement the Singleton class in Java. This pattern uses a static inner class to hold the Singleton instance and initializes it on demand, ensuring lazy initialization. As shown in the example below, the private constructor prevents instantiation of the Singleton class from outside.
🌐
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. For example, if your Singleton Frame is meant as a global user interface for your application and two are created, your application will have two Frames on the screen -- quite confusing for the user.
🌐
Tutorialspoint
tutorialspoint.com › java › java_using_singleton.htm
Java - How to Use Singleton Class?
February 24, 2020 - Since there is only one Singleton instance, any instance fields of a Singleton will occur only once per class, just like static fields. Singletons often control access to resources, such as database connections or sockets. For example, if you have a license for only one connection for your database or your JDBC driver has trouble with multithreading, the Singleton makes sure that only one connection is made or that only one thread can access the connection at a time.
Find elsewhere
🌐
Wikipedia
en.wikipedia.org › wiki › Singleton_pattern
Singleton pattern - Wikipedia
2 days ago - A singleton implementation may use lazy initialization in which the instance is created when the static method is first invoked. In multithreaded programs, this can cause race conditions that result in the creation of multiple instances. The following Java 5+ example is a thread-safe implementation, using lazy initialization with ...
🌐
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 - - The `getProperty` method provides access to the configuration settings. The Singleton Design Pattern is an essential pattern in software development, ensuring that a class has only one instance and providing a global point of access to it.
🌐
Javatpoint
javatpoint.com › singleton-design-pattern-in-java
Singleton design pattern in Java
Singleton Design Patterns with Patterns, design, Creational Design, Abstract Factory Pattern, singleton design patterns, Adapter, Visitor Pattern, Template Pattern, Command Pattern, State Pattern, java etc.
🌐
GeeksforGeeks
geeksforgeeks.org › system design › singleton-design-pattern
Singleton Method Design Pattern - GeeksforGeeks
In this method, the Singleton instance is created only when it is first requested. This helps save memory and resources, but the basic implementation is not thread-safe. Example: Classical Java implementation of singleton design pattern
Published   2 days ago
🌐
HowToDoInJava
howtodoinjava.com › home › design patterns › creational › singleton
Singleton Pattern (with Example)
November 5, 2024 - Logger Instances: Many logging frameworks, like Log4j or SLF4J, often implement Singleton patterns for their logger instances to ensure that all parts of the application use the same logger configuration and instance. Java Naming and Directory Interface (JNDI): In Java EE applications, the InitialContext class from the javax.naming package is often implemented as a Singleton.
🌐
GeeksforGeeks
geeksforgeeks.org › java › java-singleton-design-pattern-practices-examples
Java Singleton Design Pattern Practices with Examples - GeeksforGeeks
January 3, 2025 - 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. It is done by assigning the reference of an instance directly. It can be used when program will always use instance of this class, or the cost of creating the instance is not too large in terms of resources and time. ... // Java code to create singleton class by // Eager Initialization public class GFG { // public instance initialized when loading the class private static final GFG instance = new GFG(); private GFG() { // private constructor } public static GFG getInstance(){ return instance; } }
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.
🌐
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 - Use the Singleton pattern when a class in your program should have just a single instance available to all clients; for example, a single database object shared by different parts of the program.
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.

🌐
Medium
medium.com › @thecodebean › singleton-design-pattern-implementation-in-java-1fba4ecc959f
Singleton Design Pattern: Implementation in Java
September 29, 2023 - Singleton Design Pattern is a creational design pattern that ensures that a class has only one instance & provides a global point of access to that instance