English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Java Basic Tutorial

Java Flow Control

Java Arrays

Java Object-Oriented (I)

Java Object-Oriented (II)

Java Object-Oriented (III)

Java Exception Handling

Java List (List)

Java Queue (Queue)

Java Map Collections

Java Set Collections

Java Input/Output (I)/O)

Java Reader/Writer

Other Java Topics

Java Singleton Pattern

In this tutorial, we will learn about the Singleton design pattern and how to apply it in Java through examples.

Singleton (single instance) is a design pattern, not a feature unique to Java. It ensures that only one instance of a class is created.

Design patterns are like our code libraries, containing various coding techniques shared by programmers worldwide.

Java Singleton Pattern

Below is how to use the singleton method in Java.

  • Create a private constructor that restricts object creation outside the class

  • Create a private attribute that references the singleton object

  • Create a public static method that allows us to create and access the object we create. Within the method, we will create a condition to limit the creation of multiple objects.

This is an instance.

class SingletonExample {
    //Reference the private field of the object
   private static SingletonExample singleObject;
   private SingletonExample() {
       //Constructor of SingletonExample class
   
   public static SingletonExample getInstance() {
        //Write code that allows us to create only one object
        //Access the object as needed
   

In the above example,

  • private static SingletonExample singleObject - Reference to the class object.

  • private SingletonExample() - Private constructor used to restrict object creation outside the class.

  • public static SingletonExample getInstance() - This method returns a reference to the unique object of this class. Since this method is static, it can be accessed using the class name.

Use the singleton class class

Singleton can be used when using the database. They can be used to create a connection pool to access the database, while reusing the same connection for all clients. For example,

class Database {
   private static Database dbObject;
   private Database() {      
   
   public static Database getInstance() {
      //Create an object (if not already created)
      if(dbObject == null) {
         dbObject = new Database();
      
       //Return a singleton object
       return dbObject;
   
   public void getConnection() {
       System.out.println("Now connected to the database.");
   

class Main {
   public static void main(String[] args) {
      Database db1;
      //A unique object referencing Database
      db1= Database.getInstance();
      
      db1.getConnection();
   

When we run the program, the output will be:

Now we have connected to the database.

In the above example,

  • We have created a Singleton class Database.

  • dbObject is a class type field. This will refer to the object of the class Database.

  • The private constructor Database() prevents the creation of objects outside the class.

  • The static class type method getInstance() returns the instance of the class to the outside.

  • In the Main class, we have a class type variable db1We are using db1Call getInstance() to get the unique Database object.

  • The getConnection() method can only be used to access Database with the object.

  • Since Database can only have one object, all clients can access the database through a single connection.

It should be noted that only a few cases (such as logging) make Singleton meaningful. Even database connections should not usually be Singleton.

If you are unsure whether to use Singleton, we recommend that you avoid using Singleton completely.