
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
Implement Private Constructors in Java
In this article, we will understand how to implement private constructors in Java. Private constructors allow us to restrict the instantiation of a class, ensuring that objects of that class can only be created within the class itself.
Read More: Java Constructors
Problem Statement
Given a class with a private constructor, write a Java program to demonstrate how the class restricts the instantiation of objects from outside the class.
Different approaches
The following are the different approaches to demonstrate how the class restricts the instantiation of objects from outside the class.
- Using a private constructor to restrict the object creation.
- Using a private constructor in the singleton design pattern.
Using a private constructor to restrict the object creation approach
Input
The method call PrivateConstructor.instanceMethod(); inside the main() method.
Output
Invoking a call to private constructorA private constructor is being called.
Steps to implement private constructors
The following are the steps to implement private constructors?
- We define a private constructor using the ?private' keyword.
- Making a constructor private ensures that an object of that class can't be created.
- A private constructor can be used with static functions which are inside the same class.
- The private constructor is generally used in singleton design patterns.
- In the main method, we use a print statement to call the static method.
- It then displays the output on the console.
Java program to implement a private constructor
The following is an example of a Java Program to implement a private constructor ?class PrivateConstructor { private PrivateConstructor () { System.out.println("A private constructor is being called."); } public static void instanceMethod() { PrivateConstructor my_object = new PrivateConstructor(); } } public class Main { public static void main(String[] args) { System.out.println("Invoking a call to private constructor"); PrivateConstructor.instanceMethod(); } }
Output
Invoking a call to private constructor A private constructor is being called.
Using a private constructor in the singleton design pattern. approach
Input
The method calls PrivateConstructor.getInstance(); in the main() method to retrieve the Singleton instance.
Output
Invoking a call to private constructorThe value of first instance = 20 The value of second instance = 20
Steps to implement a private constructor (Singleton Pattern)
The following are the steps to implement a private constructor (Singleton Pattern) ?
- Define a private constructor using the private keyword.
- Create a static instance variable and set it to null.
- Use a static method (getInstance()) to check if the instance is null. If it is, create a new instance.
- Return the single instance each time getInstance() is called.
- Call getInstance() in the main method to work with the single instance and print the result.
Java program to implement a private constructor (Singleton Pattern)
The following is an example of implementing a private constructor (Singleton Pattern) ?
class PrivateConstructor { private PrivateConstructor () { System.out.println("A private constructor is being called."); } public static void instanceMethod() { PrivateConstructor my_object = new PrivateConstructor(); } } public class Main { public static void main(String[] args) { System.out.println("Invoking a call to private constructor"); PrivateConstructor.instanceMethod(); } }
Output
Invoking a call to private constructor The value of first instance = 20 The value of second instance = 20