in , ,

Java Singleton

Java Singleton
Java Singleton

Java Singleton

In this tutorial, we will find out about the singleton design pattern and how to apply it in Java with the help of examples.

Singleton is a design pattern as opposed to a component explicit to Java. It ensures that just one occurrence of a class is made.

A design pattern is like our code library that incorporates different coding methods shared by programmers around the globe.


Java Singleton

Here’s how we can use singletons in Java.

  • create a private constructor that restricts to create an object outside of the class
  • create a private attribute that refers to the singleton object.
  • create a public static method that allows us to create and access the object we created. Inside the method, we will create a condition that restricts us from creating more than one object.

Here’s an example.

class SingletonExample {

// private field that refers to the object
   private static SingletonExample singleObject;

   private SingletonExample() {
      // constructor of the SingletonExample class
   }

   public static SingletonExample getInstance() {
      // write code that allows us to create only one object
      // access the object as per our need
   }
}

In the above example,

  • private static SingletonExample singleObject – a reference to the object of the class.
  • private SingletonExample() – a private constructor that restricts creating objects outside of the class.
  • public static SingletonExample getInstance() – this method returns the reference to the only object of the class. Since the method static, it can be accessed using the class name.

Use of Singleton Class

Singletons can be used while working with databases. They can be used to make an association pool to get to the database while reusing a similar association for all the clients. For instance,

class Database {
   private static Database dbObject;

   private Database() {      
   }

   public static Database getInstance() {

      // create object if it's not already created
      if(dbObject == null) {
         dbObject = new Database();
      }

       // returns the singleton object
       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;

      // refers to the only object of Database
      db1= Database.getInstance();
      
      db1.getConnection();
   }
}

When we run the program, the output will be:

You are now connected to the database.

In our above example,

  • We have created a singleton class Database.
  • The dbObject is a class type field. This will refer to the object of the class Database.
  • The private constructor Database() prevents object creation outside of the class.
  • The static class type method getInstance() returns the instance of the class to the outside world.
  • In the Main class, we have class type variable db1. We are calling getInstance() using db1 to get the only object of the Database.
  • The method getConnection() can only be accessed using the object of the Database.
  • Since the Database can have only one object, all the clients can access the database through a single connection.

It’s imperative to take note of that, there are a couple situations (like logging) where singletons make sense. Indeed, even a database association normally ought not to be a singleton.

We recommend you to avoid using singletons completely if you are not sure whether to use it or not. Learn more: What is so bad about Singleton?


Thanks for reading! We hope you found this tutorial helpful and we would love to hear your feedback in the Comments section below. And show us what you’ve learned by sharing your photos and creative projects with us.

salman khan

Written by worldofitech

Leave a Reply

Java Anonymous Class

Java Anonymous Class

Java enums

Java enums