Singleton Class in Java – Two Simple Ways For Implementing

1. Objective

In our last tutorial, we had discussed Command Line Arguments in Java, and today we will discuss what is singleton class in Java. Moreover, we will study the implementation of singleton class in Java with method name as that of a class name and getInstance() method. At last, we will look at the singleton class in java examples.

So, let’s start with Java Singleton Class.

Singleton Class in Java

Singleton Class in Java – Two Simple Ways For Implementation

2. What is Singleton Class in Java?

In Object Oriented Programming, a singleton class is a class that can have just a single object (an occasion of the class) at any given moment.

After the first time, in the event that we endeavour to instantiate the singleton class, the new variable additionally indicates the principal instance made. So, whatever changes we do to any class inside the class through any case, it influences the variable of the single occasion made and is unmistakable in the event that we get to that variable through any variable of that class compose characterized.

To plan a singleton class in Java:

  • Make constructor as private.
  • Compose a static technique that has return write a protest of this singleton class. Here, the idea of lazy introduction is used to compose this static strategy.

Ordinary class versus Singleton class in Java

The major difference between typical and singleton class in Java is- For instantiation, in an ordinary class, we utilize constructor. While for singleton class in Java we utilize getInstance() technique. When all is said to be done, to maintain a strategic distance from perplexity we may likewise utilize the class name as method name while characterizing this technique.

Do you know What is Wrapper Class in Java?

3. How to Implement Singleton Class in Java?

Singleton Class in Java is implemented by two ways, let’s discuss them one by one:

a. With getInstance() method

class Singleton
{
    private static Singleton single_instance = null;
    public String s;
    private Singleton()
    {
        s = "Hello I am a string part of Singleton class";
    }
    public static Singleton getInstance()
    {
        if (single_instance == null)
            single_instance = new Singleton();
        return single_instance;
    }
}
class Main
{
    public static void main(String args[])
    {
        Singleton x = Singleton.getInstance()
        Singleton y = Singleton.getInstance();
        Singleton z = Singleton.getInstance();
        x.s = (x.s).toUpperCase();
        System.out.println("String from x is " + x.s);
        System.out.println("String from y is " + y.s);
        System.out.println("String from z is " + z.s);
        System.out.println("\n");
        z.s = (z.s).toLowerCase();
        System.out.println("String from x is " + x.s);
        System.out.println("String from y is " + y.s);
        System.out.println("String from z is " + z.s);
    }
}
Singleton Class in Java

Singleton Class in Java – With getInstance() method

Explanation:

When we first-time call getInstance() method in Singleton class in Java, it creates an object of the class with a name the object single_instance and gets back to the variable. Since single_instance is static, it is thus changed from null to some different object. Next time, if we want to call getInstance() method, since single_instance is not null, it returns to the variable, rather then by representing to Java Singleton class again.

Read about Java File Class – java.io.File Class in Java

In the main class, we represent the Java singleton class with 3 different objects x, y, z which is calling static method getInstance(). But actually, after we create object x, variables y and z point to object x. Hence, if we change the variables of object x, that reflect when we access the variables of objects y and z. Also if we change the variables of object z, that reflect when we access the variables of objects x and y.

b. With method name as that of a class name

class Singleton
{
    // static variable single_instance of type Singleton
    private static Singleton single_instance=null;
    public String s;
    private Singleton()
    {
        s = "Hello I am a string part of Singleton class";
    }
    public static Singleton Singleton()
    {
        if (single_instance == null)
        {
            single_instance = new Singleton();
        }
        return single_instance;
    }
}
class Main
{
    public static void main(String args[])
    {
        Singleton x = Singleton.Singleton();
         Singleton y = Singleton.Singleton();
               Singleton z = Singleton.Singleton();
        x.s = (x.s).toUpperCase();
        System.ot.println("String from x is " + x.s);
        System.out.println("String from y is " + y.s);
        System.out.println("String from z is " + z.s);
        System.out.println("\n");
        z.s = (z.s).toLowerCase();
        System.out.println("String from x is " + x.s);
        System.out.println("String from y is " + y.s);
        System.out.println("String from z is " + z.s);
    }
}

Explanation

First of all in the Singleton class in Java, when we call Singleton() method, it creates an object of class Singleton with name single_instance and return it to the variable. Since single_instance is static, it change from null to some different object. Single_instance is not null, so next time if we try to call Singleton() method, it returned to the variable, instead of representing the Singleton class again.

Let’s Look at Java Generics Tutorial in brief

This was all about Java Singleton Class Tutorial. Hope you like our explanation.

4. Conclusion

Hence, we studied introduction to singleton class in Java and two ways to implement Java singleton class. In addition, we discussed getInstance() method. At last, we discuss singleton class in Java example. Furthermore, if you have any query, feel free to ask in the comment section.

Related Topic- Pair Class in Java

For reference 

Leave a Reply

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