Industrial manufacturing
Industrial Internet of Things | Industrial materials | Equipment Maintenance and Repair | Industrial programming |
home  MfgRobots >> Industrial manufacturing >  >> Industrial programming >> Java

Java Singleton Pattern Explained: How to Implement and Use It Safely

Java Singleton Pattern Explained

Discover how to implement the Singleton design pattern in Java with practical examples, ensuring a single instance throughout your application.

In Java, the Singleton pattern guarantees that a class has only one instance and provides a global access point to it.

To create a robust Singleton, a class should include:


Example: Java Singleton Class Syntax

class SingletonExample {
   // Reference to the single instance
   private static SingletonExample singleObject;

   // Private constructor prevents external instantiation
   private SingletonExample() {
      // Initialization code here
   }

   // Global access point with lazy initialization
   public static SingletonExample getInstance() {
      if (singleObject == null) {
         singleObject = new SingletonExample();
      }
      return singleObject;
   }
}

Explanation of the components:


When to Use Singletons in Java

Singletons shine in scenarios that require a single shared resource, such as a database connection pool, configuration manager, or logger. For instance, a database helper can ensure only one connection is active for all clients:

class Database {
   private static Database dbObject;

   private Database() {
      // Private constructor
   }

   public static Database getInstance() {
      if (dbObject == null) {
         dbObject = new Database();
      }
      return dbObject;
   }

   public void getConnection() {
      System.out.println("You are now connected to the database.");
   }
}

class Main {
   public static void main(String[] args) {
      Database db1 = Database.getInstance();
      db1.getConnection();
   }
}

The program outputs:

You are now connected to the database.

Key takeaways from this example:

Singleton is a design pattern, not a Java language feature. It’s a reusable coding technique documented in Design Patterns: Elements of Reusable Object‑Oriented Software by Gamma et al.


Use Singletons sparingly. They’re most appropriate for stateless or shared‑state resources like logging or configuration. If you’re uncertain, consider alternatives such as dependency injection or a simple static utility class. For deeper insight, read What is so bad about Singleton?

Java

  1. Understanding Java’s final Keyword: Variables, Methods, and Classes
  2. Mastering Java Anonymous Inner Classes: Definition, Syntax, and Practical Examples
  3. Mastering Java’s ObjectInputStream: A Comprehensive Guide
  4. Mastering Java ObjectOutputStream: Serialization, Methods, and Practical Examples
  5. Mastering Java’s PrintStream Class: Print, Println, and Printf Explained
  6. Mastering Java Reader Class: Subclasses, Methods, and Practical Example
  7. Mastering Java Generics – Building Reusable, Type‑Safe Code
  8. Mastering Java File Operations with java.io – Creation, Reading, Writing & Deletion
  9. Master Java: Understanding Objects, Classes, and Core OOP Concepts
  10. Java Object Serialization: Persisting and Restoring Objects