6 Best Use Of Private Constructor In Java

Spread the love

Constructor in Java is any special method which is used to initialize objects.

The use of private constructor in Java is to ensure the application of singleton class in java which limits multiple instances of objects getting created, i.e. don’t let public creation of objects. In simple words, it locks objects and doesn’t let copies of objects getting created.

The use of a private constructor in Java other than the default constructor in java restricts class instances from getting created in other places other than its base class.

You May Like : Core Java Syllabus

Singleton class in java is a class that can have only one object at a time post initial usage.

Read below the use of private constructor in java.

How To Access Private Constructor In Java

Refer below the example on how to access private constructor in java:


public class Tester { private static Tester instance; private Tester(){ } public static Tester getInstance(){ if(instance == null){ instance = new Tester(); } return instance; } public static void main(String[] args) { Tester tester = Tester.getInstance(); Tester tester2 = Tester.getInstance(); System.out.println(tester.equals(tester2)); } }
Code language: Java (java)

No class can be sub-classed or have a subordinate plus object cant be created outside of its class.

To access Private constructor all you do is to declare a constructor as private, also accessing Private constructor the following is needed i.e

Conditions where in the constant methods are in the class, and methods are not dynamic.

How Static Constructor are used In Java

As a thumb rule the use of private constructor in java done with constructor chaining.

Then there is the use of singleton class in java which in itself is not allowed to have instances but with a method using a default constructor in java it can be used.


class PrivateConstructorDemo { private static PrivateConstructorDemo privateConstructorDemoObj = null; private PrivateConstructorDemo(){ } public static PrivateConstructorDemo getPrivateConstructorDemo() { if(privateConstructorDemoObj == null) { privateConstructorDemoObj = new PrivateConstructorDemo(); } return privateConstructorDemoObj; } public void show() { System.out.println("Hello World"); } } public class MainClass { public static void main(String args[]) { PrivateConstructorDemo obj = PrivateConstructorDemo.getPrivateConstructorDemo(); obj.show(); } }
Code language: Java (java)

As code returns
Hello World as Screen Print

Protected Constructor In Java

Use Of Private Constructor In Java
Use Of Private Constructor In Java

Unlike the default constructor in java, Protected Constructor in Java is accessible within the bundle/package or only through inheritance which is through default constructors in Java..

A protected constructor in Java can be used by calling a protected access modifier and applying it to any set of the data members, method,s and constructor.

Private Constructor In Java

Use of Private Constructor in Java is solely accessible within class. Singleton class in java is a second type of Private constructor.

singleton class in java
singleton class in java

The methods, Variables, and constructors declared “Private” can only be accessed within the defined class. This sort of binds or chains the constructor.

Constructor can be Static In Java

A constructor can be static in Java while using it means that the method belongs to the class declared not to a particular object.

Logically it’s not beneficial as the default constructor in java is implicitly called for initializing an object in Java.

You May Like : Core Java Basic Concepts

Constructor For Private Class Java

Accessing constructor for use of Private class in java is to define a public and static method that creates and returns an object from the class.

Question & Answer related to 6 Best Use Of Private Constructor In Java

Q.Why would you use a private constructor?

A. Use of Private constructor in Java prevents the creation of instances of a class in absence of instance fields or methods for example when an instance from Math class is called then it enables constructor chaining in java.
Learn default constructor in java from the above article.

Q. What is the difference between constructor chaining and local chaining?

A. Constructor Chaining in java or chaining is the method of calling one constructor with the help of another constructor while referring to the present object or the singleton class in java.

That it can be within the same class or from the base class, that is the basic difference between these two.

Q.Why do we need constructor chaining in Java?

A. Constructor Chaining is used so that parameters can be passed through a set of different constructors in Java.

Its initialization is done in a single place that lets you maintain your initializations within a single location, and letting multiple constructors be called and used.

Q.What is constructor chaining in Java with example?

A. Example:

//base class
class BUD
//base class default constructor
this(80, 90);
System.out.println(“Base class default constructor called”);
BUD(int x, int y)
System.out.println(“Base class parameterized constructor called”);
class Prototype extends BUD
this(“Java”, “Python”);
System.out.println(“Derived class default constructor called”);
Prototype(String str1, String str2)
System.out.println(“Derived class parameterized constructor called”);
public class ConstructorChaining
public static void main(String args[])
Prototype my_example = new Prototype();

Q.What is constructor chaining in Java?

A. Constructor chaining in Java refers to Calling a constructor from another constructor that is of the same class.

It lets you pass parameters through a set of different constructors, the only condition being that the initialization to be done in a single place.

Which in turn lets you maintain initializations from a single location, and lets multiple constructor chaining in java using.

In absence of chaining, two different constructors requiring a specific parameter will have to be initialized by passing twice the parameters.

And when the initialization changes, it lets be changed it in every constructor, in place of change individually.

Q.How are constructors used for creating objects in Java?

A. Constructor used for creating objects in Java can be through a block of codes similar to a method that can be created.

It’s used while creating an instance of the class, when a constructor is called, memory for that object is allocated in the memory for further processing.

It is a special type of method through which an object is utilized.
Read the above article for default constructor in java.

Q.What is true about protected constructor in Java?

A. A Protected constructor in Java means that constructor is accessible by child classes of the parent class and by the classes which are of the same package.

Q.What happens if constructor of class A is made private in Java?

A. It means that (without reflection) constructor won’t be accessed outside of a class and other classes wont be able to call it.

B. Only members of the class will be able to create its object
C. private constructor prevents making object outside of the class and we can implements singalton design pattern

Q.How do we invoke a constructor in Java?

A. Using “this()” we invoke a constructor explicitly.

This keyword in Java is a reference to the object of the current class. which refers to a field, a method or, a constructor of a class in Java.

Q.What is the use of private methods in Java?

A. Private methods in Java are the methods that have a private access modifier and access is restricted in the defining class only and is not visible even to a child class.

Which makes it ineligible from getting overwritten, through it you can define a method within the same name in the child class and through it can access it in the parent class.

Q.What is the use of private constructor in abstract class?

A. Abstract class constructor is invoked to create an object from a derived class that inherits the abstract class.

Along with the abstract class, it has some member variables which can be initialized in other class of constructor.

Q. What happens if a constructor is declared private?

A. When a constructor is declared private then these are two options:

A.  We won’t be able to create an object of a class

B. We may use this private constructor in the Singleton class in java.
Read the above article for singleton class in java.


Constructors are just like methods with the following characteristics

  • Don’t have any return type.
  • The name of the constructor is the same as the name of the class.
  • Each class has its own constructor.
  • If no explicit constructor is written for a class, the Java compiler builds the default constructor for that class.
  • When a new object is created each time one constructor will be invoked by default as the method.
  • A class can have more than a single constructor.

Through this() and super() constructors are called explicitly.

Constructor chaining is calling a constructor of one class from other class.

From a default constructor in java, the parameterized constructors of the same class can be called by using this() and from the sub class constructor of the super class using super() can be called.

If You find this article helpful, please share with your friends, or if you have any question or suggestion you can write on below comment section

Constructing Chain in Java

Spread the love

Leave a Reply

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

Related Posts

Begin typing your search term above and press enter to search. Press ESC to cancel.

Back To Top