Online Tutorials & Training Materials | STechies.com
Register Login

Private Constructor in Java with Example

|| 0

Private Constructor in Java with Example
Stechies

What is Private Constructor in Java?

A constructor is a function used to initialize objects. A private constructor is a method that can be accessed only inside a class. It is useful when the object creation needs to be restricted. It is used primarily for singleton classes.

public class PvtConstrt{
   private static PvtConstrt abc=null;
   private PvtConstrt(){
      /*it restricts further access of class*/
   }
   public static PvtConstrt abcectCreationMethod(){
	if(abc==null){
	    abc= new PvtConstrt();
	}
        return abc;
   }
   public void display(){
	System.out.println("Private Construtctor Example");
   }
   public static void main(String args[]){
	PvtConstrt myabcect= PvtConstrt.abcectCreationMethod();
	myabcect.display();
   }
}

Uses of Private Constructor

The uses of private constructor are given below:

  • Used in singleton classes to limit the number of objects created to one. Private constructor makes sure that at a time only one object is created through the class.
  • The objects can only be created within the singleton class. No external class can create objects for this class. For example,
  • They are also used for internal chaining of constructors. For example,

Example 1

public class akalton {
   private static akalton abc=null;
   private akalton(){
   }
   public static akalton abcectCreationMethod(){
	if(abc==null){
	    abc= new akalton();
	}
        return abc;
   }
   public void display(){
	System.out.println("Singleton  Example");
   }
   public static void main(String args[]){
	akalton myabcect= akalton.abcectCreationMethod();
	myabcect.display();
   }
}

Example 2

public class Hell{
   String name;
   //Constructor
   Hell(){
      this.name = "Java books are available";
   }
   public static void main(String[] args) {
      Hell obj = new Hell();
      System.out.println(obj.name);
   }
}

Example 3

public class Cheking {
   private static Cheking instance;
   private Cheking(){}
   public static Cheking getInstance(){
      if(instance == null){
         instance = new Cheking();
      }
      return instance;
   }
   public static void main(String[] args) {
      Cheking cheking = Cheking.getInstance();
      Cheking cheking1 = Cheking.getInstance();
      System.out.println(cheking.equals(cheking1));
   }  
}

How to call a Private Constructor?

The method java.lang.Class.getDeclaredConstructor() is used to call a private constructor. It can be used to get the object created by the private constructor of the respective class. This object represents the specific interface or constructor that corresponds to the class object. The method takes in an array of class object that depicts the parameter types of the constructor.

import java.lang.reflect.Constructor;

public class refl_Example {
    public static void main(final String[] args) throws Exception {
        Constructor<Mnop> constructor = Mnop.class.getDeclaredConstructor();
        constructor.setAccessible(true);
        Mnop Mnop = constructor.newInstance();
        System.out.println(Mnop);
    }
}
class Mnop {
    private Mnop() {
        // private!
    }
    @Override
    public String toString() {
        return "I'm a Tall persone !";
    }
}

A static method is the other way to call Private Construction, private constructors cannot be access from the outside the class. 

public class ClassToAccess{
    public static void main(String[] args){
            ClassWithPrivateConstructor obj = ClassWithPrivateConstructor.getObj();
            obj.printsomething();
    }

 


Related Articles