Singleton_Design_pattern-min

singleton class in java

In Java, it is common to have a class with only one instance. In this article, we will learn about what exactly is a Singleton Class in Java?

We will also learn about the different ways to implement a Singleton Class in Java. The concept of the singleton class is one of the commonly used design patterns in object-oriented programming languages like Java and C++.

singleton class in java

This pattern ensures that there can be only one instance of a class in memory at any point of time by making it a static object. If you are wondering how do you create singleton classes, read further.

What is a Singleton Class?

A singleton class is a class which has only one instance in memory. Basically, this is achieved by making the class static and static variables.

If the static variable is assigned a new value in any method or program, it will get deleted from memory and there will only be one copy of the class in memory.

At the same time, there is no way for a client to create multiple copies of this class. A pure singleton class has these two characteristics:

It is a static class. The main reason behind making a class as a singleton is to have only one instance in memory. It is a pure singleton class. A pure singleton class is one which has only two characteristics: it is a static class and it has only one instance in memory.

How to create singleton class in Java?

To create a singleton class in Java, we need to implement two interfaces. These two interfaces are: @ interface – An interface is nothing but a contract between the client code and the implementation code. In other words, it is the contract which is implemented between the classes.

Use interface keywords to define an interface in Java. An interface defines the contract between the client code and the implementation code.

The client code that uses the interface has no knowledge about the implementation code. For example, if we have a class which handles the request of an employee, it will implement the Employee interface.

Therefore, all the classes which implement this interface will have request like method inside it. This is called polymorphism.

Using an enum for Singleton Class

In the above section, we discussed about the singleton class that needs to handle multiple individual request but at the same time, it should not have a huge memory footprint.

We can achieve this by creating an enum to hold the different values of the employee. We will create an enum with a static variable of each enum value.

For example, we can define an enum type named EmployeeType with the values such as SalesManager, JuniorProgrammer, etc.

We can create a static variable of enum type named enumEmployeeType with the name of enumEmployeeType variable.

Now, we can assign the value of enumEmployeeType variable from inside any method. This way, we can create a singleton class with a static variable.

Using static variable for Singleton Class

In the above section, we discussed about the singleton class that needs to handle multiple individual request but at the same time, it should not have a huge memory footprint.

We can achieve this by creating an enum to hold the different values of the employee. We will create an enum with a static variable of each enum value.

For example, we can define an enum type named EmployeeType with the values such as SalesManager, JuniorProgrammer, etc.

We can create a static variable of enum type named enumEmployeeType with the name of enumEmployeeType variable.

Now, we can assign the value of enumEmployeeType variable from inside any method. This way, we can create a singleton class with a static variable.

Using final variable for Singleton Class

There is a feature in Java which allows us to create final variables. We can make a final variable as static or final. A final variable cannot be changed from inside the program.

A static final variable will not be deleted even if we delete the code which has a final variable assigned to it. We can create a singleton class with final variables.

A final variable is one which is not deleted even if we delete the code which has a final variable assigned to it.

For example, we can define an enum type named EmployeeType with the values such as SalesManager, JuniorProgrammer, etc.

We can create a static variable of enum type named enumEmployeeType with the name of enumEmployeeType variable.

Now, we can assign the value of enumEmployeeType variable from inside any method. This way, we can create a singleton class with a static variable.

Using Instance Variables to make our class as a singleton

There is a feature in Java which allows us to create instance variables in a class. We can make a method as an instance variable in a class.

We can create instance variables for returning various values from inside our class. These variables can be of any type such as String, Integer, etc.

We can create instance variables in a class and then initialize their values inside the class constructor. We can also make a public instance variable and make it final.

For example, we can define an enum type named EmployeeType with the values such as SalesManager, JuniorProgrammer, etc. We can create instance variables in a class and then initialize the values inside the class constructor.

Using constructor parameter to make our class as a singleton

There is a feature in Java which allows us to pass a parameter to a constructor. We can pass a parameter to a constructor to initialize the instance variables in the constructor’s first parameter.

We can make a constructor as static or non-static. A static constructor is one which is not visible outside the class. A non-static constructor is one which is visible outside the class.

For example, we can define an enum type named EmployeeType with the values such as SalesManager, JuniorProgrammer, etc.

We can make a constructor as static or non-static. A static constructor is one which is visible outside the class. A non-static constructor is invisible outside the class.

Singleton class in Java example –Employee singleton

We can create any type of singleton class in Java with above mentioned techniques. Let’s now write a singleton class named Employee which will have only one instance in memory.

The following code shows how to create a singleton class named Employee in Java. The code will be explained below.

Now, our singleton class named Employee will have only one instance in memory. We can create multiple instances of Employee class but they will have only one instance of Employee class in memory.

Conclusion

Hope you enjoyed reading this article on what is a Singleton Class in Java? We also discussed about the singleton class implementation in Java such as pure singleton class implementation,

enum for singleton class implementation, static variable for singleton class implementation, final variable for singleton class implementation, instance variable for singleton class implementation, constructor parameter for singleton class implementation and finally making our class as a singleton by using instance variable to make our class as a singleton.

You can also request any other implementation of singleton class from us. Please do let us know in the comment section.

Add a Comment

Your email address will not be published. Required fields are marked *