Table of contents
1.
Introduction
2.
What is Private Constructors in Java?
3.
Syntax of Java Private Constructors
4.
What is Singleton Class in Java?
4.1.
Java
5.
Frequently Asked Questions
5.1.
Where is the concept of Singleton class mostly used?
5.2.
Can a Singleton class have a public Constructor?
5.3.
Why is the instance member of a Singleton class declared static?
5.4.
Can we declare constructor as final?
6.
Conclusion
Last Updated: Oct 7, 2024
Easy

Private Constructors and Singleton Classes in Java

Author Pradeep Kumar
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

In Java, private constructors and singleton classes are closely related concepts. Private Constructors and Singleton Classes in Java are used to restrict the instantiation of a class to a single instance. A private constructor prevents direct instantiation of the class from outside, while a singleton class ensures that only one instance of the class exists throughout the lifetime of the program, providing global access to that instance.

Private Constructors and Singleton Classes in Java

What is Private Constructors in Java?

In Java, you can declare a constructor as private. By declaring the constructor of a class as private, you can not create an object of that class. To declare the constructor as private, you can use the private access specifier. We generally use a private constructor in a Singleton class.

Syntax of Java Private Constructors

class class_name {
    // body of the class

    // declaring the constructor as private
    private class_name(){
        // Code for the constructor
    } 
}

 

What is Singleton Class in Java?

A singleton class is one that can only have one object (a class instance) at a time. When we try to instantiate the Singleton class again, the new variable also points to the initial instance produced. As a result, any changes we make to any variable within the class through any instance affect the variable of the single instance produced, which can be seen if we access that value through any variable of the class type stated. We use a private constructor in a singleton class. For example,

Main.java

  • Java

Java

// Singleton Class
class SingletonClass
{
   private static SingletonClass class_instance = null;
   public String s = "Initial Value";
 
   // private constructor
   private SingletonClass () {}
 
   // method to get the instance of the class
   static public SingletonClass getClassInstance()
   {
       if (class_instance == null){
           class_instance = new SingletonClass ();
           return class_instance;
       }       
       else{
           return class_instance;
       } 
   }
}

// Main Class
class Main
{
  public static void main(String args[])   
  {
       SingletonClass first_instance = SingletonClass.getClassInstance();
       SingletonClass second_instance = SingletonClass.getClassInstance();
           //print the value of s from both instances
       System.out.println("value of s in first_instance: " + first_instance.s + " and value of s in second_instance is: " + second_instance.s);
       //now change the value of s in first_instance
       first_instance.s = "Changed Value";
       //now print the value of string from both the instances again
       System.out.println("value of s in first_instance: " + first_instance.s + " and value of s in second_instance is: " + second_instance.s);
  }   
}
You can also try this code with Online Java Compiler
Run Code

 

Output:

value of s in first_instance: Initial Value and value of s in second_instance is: Initial Value
value of s in first_instance: Changed Value and value of s in second_instance is: Changed Value

 

As you can see from the output, upon changing the value of variable 's' in one instance, the value changes in another instance as both instances are pointing to one object. Try this code by yourself on Online Java Compiler.

Frequently Asked Questions

Where is the concept of Singleton class mostly used?

In concepts like networking and database connectivity, singleton classes are frequently used.

Can a Singleton class have a public Constructor?

If a class has a public constructor, then by definition, anybody can create an instance of it at any time, so it can't be a singleton.

Why is the instance member of a Singleton class declared static?

The instance field contains the unique reference to the single instance of the Singleton class. Because its scope must be the class itself and not a specific instance, it is stored in a static variable.

Can we declare constructor as final?

No, constructors in Java cannot be declared as final. Declaring a constructor as final is not allowed because constructors are not inherited, and the final keyword is used to prevent methods from being overridden and classes from being inherited.

Conclusion

In this article, we talked about Private Constructors and Singleton Classes in Java. We learned that private constructors prevent direct instantiation of a class from outside, while singleton classes ensure only one instance of the class exists. These concepts are useful for controlling object creation, maintaining global state, and providing a single point of access to resources throughout the program.

We hope that this blog has helped you enhance your knowledge regarding the Singleton class and Private Constructors, and if you like to learn more, check out our article on Writer Class. And to learn in-depth about android development, check out our Android Development course on the Code360.

Recommended articles

Live masterclass