- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- 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 have a constructor private in java?
A constructor is used to initialize an object when it is created. It is syntactically similar to a method. The difference is that the constructors have same name as their class and, have no return type.
There is no need to invoke constructors explicitly these are automatically invoked at the time of instantiation.
Access specifiers/modifiers allowed with constructors
Modifiers public, protected and, private are allowed with constructors.
We can use a private constructor in a Java while creating a singleton class. The Singleton's purpose is to control object creation, limiting the number of objects to only one. Since there is only one Singleton instance, any instance fields of a Singleton will occur only once per class, just like static fields. Singletons often control access to resources, such as database connections or sockets.
Accessing the private constructor
To access a private constructor (One way to do so) define a public and static method which creates and returns an object of the class (with private constructor).
Now you can get the instance by invoking this method.
Example
In the following Java program, we have a class with name Student whose constructor is private.
In the Student class we have a method with name getInstance() which is both public and static. This method creates an object of the Student class and returns it.
From another class we are invoking this (getInstance()) method and with the obtained instance/object we are calling the display() method of the Student class.
class Student{ private String name; private int age; private Student(){ this.name = "Raju"; this.age = 20; } public void display(){ System.out.println("Name of the Student: "+this.name ); System.out.println("Age of the Student: "+this.age ); } public static Student getInstance() { Student object = new Student(); return object; } } public class PrivateConstructorExample{ public static void main(String args[]) { Student obj = Student.getInstance(); obj.display(); } }
Output
Name of the Student: Raju Age of the Student: 20
- Related Articles
- Can we declare a constructor as private in Java?
- Can we have a private method or private static method in an interface in Java 9?\n
- Where and how can I create a private constructor in Java?
- Can we define a static constructor in Java?
- How to call Private Constructor in Java
- Can we override private methods in Java\n
- Can we override a private or static method in Java
- Can we declare constructor as final in java?
- Can we declare a main method as private in Java?\n
- Can we call a constructor directly from a method in java?
- Can we initialize static variables in a default constructor in Java?
- Can we define constructor inside an interface in java?
- What is the purpose of private constructor in Java?
- Can we define a parameterized constructor in an abstract class in Java?
- Can we use private methods in an interface in Java 9?
