Private Constructor in Java | Use, Example

When a constructor is declared with a private access modifier in a class, it is called private constructor in java. It is visible within the same class where it has been declared.

Java private constructor is useful when we want to prevent users from creating an object of class. Only within class, we can make a new object of class with private constructor.

For example, classes such as (Math class or System class) have private constructor. They can never be instantiated from outside of the class.

This is because all of its data fields and methods are static in nature. To prevent users from creating objects of Math class, the constructor in java.lang.Math is declared as follows:

private Math(){

}

How to make Constructor Private


The syntax to make a constructor private in a class is as follows:

class Student {
 private Student(){ // private constructor.

  }
}

What happens when we make Constructor private in Java


There are two cases happen when we make a constructor private in a class. They are as follows:


1. A class cannot be extended when a constructor is declared as private. For example:

class School {
 private School() {

 }
}
class Student extends School { // Error: cannot extend a class 'School'because class constructor is marked as private.

}

2. We cannot create an instance of class when we declare constructor private. If we try, compile-time error will get.

new School(); // Error: Constructor of class 'School' is private and only accessible within the class declaration.

Use of Private Constructor in Java


There are mainly two important uses of private constructor in java. They are as follows:

  1. Private constructor is used when we want to prevent other classes from creating an object of a class.
  2. It can be used in single tone classes where the object of the class cannot be created outside the class.

Example Program based on Private Constructor


Let’s take some useful program where we declare a constructor private in a class and see what happens?


Program source code 1:

public class Test 
{
  String name;
  int age;

private Test(String name, int age) {
 this.name = name;
 this.age = age;
}
void display(){
 System.out.println("Name: " +name);
 System.out.println("Age: " +age);
}
public static void main(String[] args) 
{
// Creating an object of class with private constructor within the same class.
   Test t = new Test("John", 35);
   t.display(); // Calling method.
  }
}
Output:
      Name: John
      Age: 35

Program source code 2:

public class Student 
{
  String name;
  int age;

private Student(String name, int age) {
 this.name = name;
 this.age = age;
}
void display(){
 System.out.println("Name: " +name);
 System.out.println("Age: " +age);
 }
}
public class DisplayInfo {
 public static void main(String[] args) {
// Creating an object of class Student with private constructor from outside class.
   Student st = new Student("John", 20); // Compilation error.	 
    st.display();
  }
}
Output:
      Exception in thread "main" java.lang.Error: Unresolved compilation problem: 
	The constructor Student(String, int) is not visible

Program source code 3:

public class Student 
{
  String name;
  int age;

private Student(String name, int age) {
 this.name = name;
 this.age = age;
}
void display(){
 System.out.println("Name: " +name);
 System.out.println("Age: " +age);
 }
}
public class DisplayInfo extends Student {
DisplayInfo(){
  super("John", 25); // Compilation problem. 
 }
 public static void main(String[] args) {
// Creating an object of class Student with private constructor from outside class.
   DisplayInfo dis = new DisplayInfo();	 
    dis.display();
  }
}
Output:
       Exception in thread "main" java.lang.Error: Unresolved compilation problem: 
	The constructor Student(String, int) is not visible

Frequently Asked Questions in Google Search

1. Can a constructor be private in Java?
2. What is the use of private constructor?
3. How to access or class private constructor?
4. Can we call private constructor?

Hope that this tutorial has covered all important points related to private constructor with example programs. I hope that you will have understood this simple topic.
Thanks for reading!!!
Next ⇒ Java Constructor Overloading
⇐ PrevNext ⇒