Private Constructor in Java
Overview
A private constructor in Java is simply a constructor with the private keyword in it. Private constructors help in creating singleton classes (Singleton classes are classes that allow only one instance to be created).
No more than one object can be created using a private constructor of a class.
A constructor is a special method in Java with the same name as the class, automatically invoked when an object of the class is created. The difference between a simple constructor and a private constructor in java is that a simple constructor allows object creation outside the class, while a private constructor restricts it to within the class.
Static methods are used to access private constructors outside the class.
What is a Private Constructor in Java?
Simple Constructor
Code
Output
Private Constructor
Code
Output
Explanation A few things we have to look at in the above code are:
- We have created a private constructor in java program inside the Scaler class itself.
- The private is the additional keyword used while creating the private constructor.
- The object of the Scaler class is instantiated within the Scaler class (same class).
As private elements need to be accessed only inside the class in which they are defined. So to call the private constructor Scaler we need to create an object of the scaler class within itself.
Accessing Private Constructor Outside the Class
Code
Output
Accessing Private Constructor Outside the Class Using Static Methods
Code
Output
Rules of Private Constructor
The following rules must be followed while dealing with a private constructor.
- Prevents subclassing of the class (no inheritance).
- Restricts object construction outside the class.
- Compile-time error when trying to extend a class with a private constructor in java.
- Private constructor in java inaccessible from other classes.
- Useful when class has only constant methods or all methods are static.
- Private constructor in java can be used to control object initialization through public functions.
- Private constructor in java ensures only one instance is returned when object is already initialized.
Use Cases of Private Constructor
- Private constructors in java limit object generation.
- They are used to implement the singleton design pattern.
- Useful for static member-only classes.
- Employed for factory methods instead of direct constructors.
- Help avoid sub-classing and restrict object creation.
- Singleton classes allow only one instance to exist.
Implementation of Private Constructor
Code
Output
Typical Usage
Restricting explicit class instantiation has numerous patterns and benefits, and we'll go over the most popular ones in this tutorial:
The Singleton Pattern
Code
Output
Explanation
- Singleton pattern ensures only one instance of a class exists at a time.
- Accessing getInstance() method returns the same instance each time.
- Hashcodes of variables p, q, and r are the same, indicating the same instance is returned.
Delegating Constructors
Code
Output
Explanation
- Delegating constructors call another constructor of the same class with different parameters.
- Private constructors can be accessed using public constructors.
- In the example, public constructors for int and boolean call a private constructor, passing appropriate values and types.
Uninstantiable Classes
Code
Output
Explanation
- Uninstantiable classes have private constructor in java, making them inaccessible outside the class.
- The class A consists entirely of static methods, so it cannot be instantiated.
- Static methods can be accessed directly from the class without creating an object.
The Builder Pattern
Code
Explanation
- The builder pattern is used to create complex objects step by step.
- The Employee class has a private constructor, and an inner Builder class handles object creation.
- The Builder class has methods to set the Name, EmployeeID, and Department.
- Calling build() method returns an Employee object with the specified details.
Conclusion
- The use of private constructor in java restricts object creation outside the class.
- One can create the object of the class has a private constructor by using static methods.
- A private constructor in java is mostly used to create singleton classes.
- To avoid sub-classing, one must include a private constructor in their class.