DEV Community

Cover image for Private Constructor in JAVA
Abhishek Jaiswal
Abhishek Jaiswal

Posted on

Private Constructor in JAVA

In JAVA the constructor is a special type of method that has the same name as the class name or file name.
And when we call an object then internally constructor always called, basically it is use to initialize the state of object or the variables values in the class.

Now Let's see what is private constructors

JAVA allows us to modify the methods based on the our use.
So can we make constructor as a private like other methods? and if YES! then what is the use of private constructor?.
Let's check it out!!

So the answer is yes we can declare a constructor in java as a private constructor by using private access specifier. And the most important thing comes into picture that if we declare a constructor as a private then we are not able to create an object of the class. Instead we can use this private constructor in Singleton Design Pattern.

Let's Check it out the Rules for Private Constructors

  • It doesn't allow a class to be sub-classed
  • It doesn't allow to create an object outside the class
  • If a class has a private constructor and when we try to extend the class, a compile-time error occurs.
  • We cannot access a private constructor from any other class.
  • If all the constant methods are there in our class, we can use a private constructor.
  • If all the methods are static then we can use a private constructor.
  • We can use a public function to call the private constructor if an object is not initialized.
  • We can return only the instance of that object if an object is already initialized.
public class PrivateConstructorDemo   
{  
     //creating an instance variable of the class Tester  
     private static PrivateConstructorDemo pcd;  
     //creating a private constructor  
     private PrivateConstructorDemo()  
     {  
     }  
    //creating a static method named getInstance()  
    public static PrivateConstructorDemo getInstance()  
    {  
       if(pcd == null)  
       {  
        //creating a constructor of the class      
        pcd = new PrivateConstructorDemo();  
       }  
     return pcd;  
    }  
    //main() method  
    public static void main(String args[])   
    {  
     PrivateConstructorDemo pcd = PrivateConstructorDemo.getInstance();  
    PrivateConstructorDemo pcd1 = PrivateConstructorDemo.getInstance();  
    //invokes the getInstance() method and prints the corresponding result  
    System.out.println(pcd.equals(pcd1));  
   }    
}  
Enter fullscreen mode Exit fullscreen mode

output

true
Enter fullscreen mode Exit fullscreen mode

Let's see the Use Cases of Private Contractors

The main purpose of using a private constructor is to restrict object creation. We also use private constructors to implement the Singleton design pattern. The use-cases of the private constructor are as follows:

  • It can be used with static members-only classes.
  • It can be used with static utility or constant classes.
  • It can also be used to create singleton classes.
  • It can be used to assign a name, for instance, creation by utilizing factory methods.
  • It is also used to avoid sub-classing.
  • It incorporates the factory methods.

Now it's time for you to explore more about the JAVA constructors.

Top comments (0)