
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Can we declare a constructor as private in Java?
Java constructor is a special method used to initialize objects. It has the same name as the class and is automatically called when an object is created.
You can also declare a constructor as private to restrict object creation from outside the class.
What is a Private Constructor?
A private constructor is a constructor that is declared using the private access modifier. It restricts object creation from outside the class i.e., the class having a private constructor cannot be subclassed.
Private constructors are used in design patterns like Singleton, or to prevent subclassing or instantiation.
Purpose of a Private Constructor
- A private constructor restricts object creation from other classes.
- A private constrictor restricts inheritance, as the subclass cannot call the private constructor of the parent class.
- A private constructor is generally used when the class contains only static members or utility methods.
Declaring a Private Constructor
The declaration of a private constructor is similar to that of a regular constructor, but it uses the private access modifier.
Syntax
Below is the syntax to declare a private constructor:
class Tutorialspoint { // We are declaring it as private constructor private Tutorialspoint() { // private constructor. // Constructor code here. } // Other methods and fields. }
Conditions for Private Constructor
Below are the conditions for the private constructor:
- A private constructor does not allow to create an object outside the 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.
- If we try to extend a class which is having private constructor compile-time error will occur.
Example of Private Constructor
Below is a simple example of using the private constructor:
package Tutorialspoint; public class Test { String employeename; int employeeage; // Declaring constructor as private. private Test(String employeename, int employeeage) { this.employeename = employeename; this.employeeage = employeeage; } void display() { System.out.println("Name: " + employeename); System.out.println("Age: " + employeeage); } // Main method. public static void main(String[] args) { Test t = new Test("Krishna", 35); t.display(); // Calling the method. } }
The output of the above example is:
Name: Krishna Age: 35
Example of Access Restriction with Private Constructor
Below is an example to create a private constructor that will throw a compile-time error:
public class A { int a; double b; String c; private A() { a = 10; b = 20.2; c = "Alfa"; System.out.println(a + " " + b + " " + c); } } class B { public static void main(String[] args) { A e = new A(); } }
In the above program, as we are calling the constructor A() inside the class B, whereas it should be defined in class A, as a result, it will throw an error.
The output of the above example is:
Warnings/Errors: A.java:17: error: A() has private access in A A e=new A(); ^ 1 error
Singleton Design Pattern Using Private Constructor
The class that can only have one object at a time is known as a singleton class. We use private constructors to implement the singleton design pattern so that we can restrict the instantiation of the class from other classes.
Example
Below is an example of creating a private constructor in the Singleton Design Pattern:
class SingletonObject { private SingletonObject() { System.out.println("In a private constructor"); } public static SingletonObject getObject() { // we can call this constructor if (ref == null) ref = new SingletonObject(); return ref; } private static SingletonObject ref; } public class PrivateConstructorDemo { public static void main(String args[]) { SingletonObject sObj = SingletonObject.getObject(); } }
The output of the above example is:
In a private constructor