Java tutorials for Beginners – Java Singleton

(Java programming Example for Beginners)

Java Singleton

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

Singleton is a design pattern rather than a feature specific to Java. It ensures that only one instance of a class is created.

A design pattern is like our code library that includes various coding techniques shared by programmers around the world.


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 create a connection pool to access the database while reusing the same connection for all the clients. For example,


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 important to note that, there are only a few scenarios (like logging) where singletons make sense. Even a database connection usually should not be a singleton.

 

Java tutorials for Beginners – Java Singleton

Sign up to get end-to-end “Learn By Coding” example.



Disclaimer: The information and code presented within this recipe/tutorial is only for educational and coaching purposes for beginners and developers. Anyone can practice and apply the recipe/tutorial presented here, but the reader is taking full responsibility for his/her actions. The author (content curator) of this recipe (code / program) has made every effort to ensure the accuracy of the information was correct at time of publication. The author (content curator) does not assume and hereby disclaims any liability to any party for any loss, damage, or disruption caused by errors or omissions, whether such errors or omissions result from accident, negligence, or any other cause. The information presented here could also be found in public knowledge domains.