🌐
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   2 days ago
🌐
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.
People also ask

Is the Singleton pattern suitable for all scenarios?
No, the Singleton pattern is not suitable for all scenarios. It is useful to manage global resources and ensure a single instance. It might not be suitable where flexibility and multiple instances are needed.
🌐
simplilearn.com
simplilearn.com › home › resources › software development › java tutorial for beginners › java singleton class: benefits & implementation
Java Singleton Class: A Complete Guide
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
🌐
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
2 days ago - 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
🌐
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
Top answer
1 of 4
3

It's not great...

My question is, is this pattern a good move? Also, is the class term "PrinterFactory" even applicable since it only constructs one instance in its entire existence within the process lifetime?

The problem I see with this implementation is that there are two cooks in charge of making the same broth.

A factory's job is to provide a public point to generate Foo from. You use it when consumers cannot feasibly create their own Foo "in the field" (as it were), and the factory provides that ability for them.

A singleton is designed to reduce instantiation to a single instance; but it almost inherently also comes with a way to access this single instance. In effect, it also acts as a public point to "generate" Foo from. By that I mean that consumer's don't actively care whether they're getting a newly created Foo or a reused pre-existing Foo.

There is no need to develop this responsibility twice. The two cooks are battling over the same broth.


... but it's not impossible either.

That being said, it is possible that this is a meeting of what was once two separate features.

For example, let's say this is about data access. You used to use a library that required generating new connection objects, and therefore you wrote a factory that generated these objects. Your entire codebase now uses that factory.

Today, you are changing your DB provider, and the new provider requires you to reuse the same connection object (for the purposes of this example, it is done via a singleton).

You don't want to rewrite your entire codebase to no longer use the factory and use the singleton instead, so you come up with a different solution: the factory simply returns the singletone. This way, you don't need to change your old code, and this all works.


Conclusion

The conclusion here is that having a factory return a singleton is somewhat superfluous and is better avoided when you're building something from scratch, simply as a matter of not doing the same thing twice or muddying the line between the responsibilities.

However, if you're trying to not have to rewrite legacy code, it can make sense to end up making a factory return a singleton just to avoid having to remove prior dependencies to the factory.

I would try to avoid it if you can, but I wouldn't go as far as to say that it should never exist either.

2 of 4
4

Having a Singleton for a printer is dubious. You state that only one printer can be connected at a time, but that is a very dubious assumption. It's wrong on every single computer that I own. Plus it's wrong once you accept things like a PDF or Postscript file generator, that is used identical to a printer.

It's double dubious when the application can be launched and someone tries to print while the printer is unplugged or turned off. Singletons are supposed to live from the moment they are first requested to the moment the application dies. If a printer can be turned off and on, how do you handle this? If a printer can be unplugged and another plugged in, how do you handle this.

It's triple dubious to make even the individual printer subclasses Singletons. If I can plug in two different printers, what would keep me from plugging in two identical printers?

Rule #1 when not to use Singletons: If you need more than one.

🌐
GeeksforGeeks
geeksforgeeks.org › system design › singleton-design-pattern-in-java
Singleton Design Pattern in Java - GeeksforGeeks
January 11, 2024 - The Singleton Design Pattern in Java ensures that a class has only one instance and provides a way to access it globally.
🌐
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!"); } }
🌐
Medium
medium.com › @vigneshwaransekartech › singleton-pattern-in-java-real-world-analogy-and-code-example-0cb931ac712c
Singleton Pattern in Java — Real-World Analogy and Code Example | by Vigneshwaran Sekar | Medium
June 30, 2025 - In system design, ensuring controlled access to resources is crucial. One design pattern that helps achieve this is the Singleton Pattern — it ensures that a class has only one instance while providing a global access point to it. Let’s explore what it is, where we use it, and how to implement it in Java.
🌐
DEV Community
dev.to › ayshriv › mastering-the-singleton-design-pattern-in-java-a-complete-guide-2ac8
Mastering the Singleton Design Pattern in Java – A Complete Guide - DEV Community
February 28, 2025 - When a class ensures that only one instance is created and used throughout the application, it is called the Singleton Design Pattern.
🌐
Medium
medium.com › @singh.piyush › java-singleton-pattern-how-to-implement-avoid-failures-and-ensure-success-599e65c1106c
Java Singleton Pattern: How to Implement, Avoid Failures, and Ensure Success | by Piyush Kumar Singh | Medium
September 18, 2024 - If you serialize and deserialize a Singleton class, a new instance can be created when the object is deserialized. This breaks the Singleton pattern. Java Reflection can access private constructors, meaning someone could use reflection to create a second instance of the Singleton class.
🌐
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.
🌐
Medium
medium.com › @lukeoregan1994 › singleton-pattern-in-java-a-complete-guide-with-best-practices-996510ab5598
Singleton Pattern in Java: A Complete Guide with Best Practices | by Lukeoregan | Medium
January 4, 2026 - Singleton Pattern in Java: A Complete Guide with Best Practices Introduction The Singleton pattern is one of the most well-known design patterns in software development. It ensures that a class has …
🌐
Medium
medium.com › @adityashete009 › implementing-singleton-classes-in-java-d59286e63cdb
Implementing Singleton Classes in Java | by Aditya Shete | Medium
May 22, 2024 - These steps effectively guarantee a thread-safe lazy-loading singleton. This manner of initialization is known as Initialization-on-demand holder idiom. All of these methods work but they are all susceptible to a particular ability of Reflection in Java. You can access private constructors, make them accessible all at runtime.
🌐
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).
🌐
Medium
rameshfadatare.medium.com › java-singleton-pattern-best-ways-to-implement-singleton-in-java-d6c526ced5d0
Java Singleton Pattern — Best Ways to Implement Singleton in Java | by Ramesh Fadatare | Medium
February 20, 2025 - Java Singleton Pattern — Best Ways to Implement Singleton in Java Introduction The Singleton Design Pattern is one of the most commonly used creational design patterns in Java. At its core, it …