The use of private constructor is to serve singleton classes. A singleton class is one which limits the number of objects creation to one. Using private constructor we can ensure that no more than one object can be created at a time. By providing a private constructor you prevent class instances from being created in any place other than this very class. We will see in the below example how to use private constructor for limiting the number of objects for a singleton class.
Example of Private Constructor
public class SingleTonClass { //Static Class Reference private static SingleTonClass obj=null; private SingleTonClass(){ /*Private Constructor will prevent * the instantiation of this class directly*/ } public static SingleTonClass objectCreationMethod(){ /*This logic will ensure that no more than * one object can be created at a time */ if(obj==null){ obj= new SingleTonClass(); } return obj; } public void display(){ System.out.println("Singleton class Example"); } public static void main(String args[]){ //Object cannot be created directly due to private constructor //This way it is forced to create object via our method where //we have logic for only one object creation SingleTonClass myobject= SingleTonClass.objectCreationMethod(); myobject.display(); } }
Output:
Singleton class Example
Paru says
Hi,
when I tried the above example, I was able to instantiate the class using new keyword,
i.e, SingleTonClass obj3=new SingleTonClass();
I presumed this wud not be possible for a private Constructor.
Pls advice if my assumption is wrong and why if that is the case.
Thanks,
Paru
Daryll David says
“SingleTonClass obj3=new SingleTonClass();” is inside the main method which is in the class SingleTonClass. Compilation succeeds because although it’s a private constructor, private members can be accessed within the class only.
remove the main method from the SingleTonClass and create a separate .java file with main method and test “SingleTonClass obj3=new SingleTonClass();”
omar says
I was going to say that , thanks !
Sandeep Saproo says
private Constructor is used to prevent object creation outside the class.
We can create more than one object in Singleton class.
Trinanjana says
Can you please explain the difference between a static class and a singleton class?
Ashir says
The static class means that call the method without creating it instance of object. In static class you freely called the method without making refrernce of object but in same class you to do….
The singleTOn class in which you have to follow the private constructor rule ..
Gautham says
What is the use of not creating more than one object at a time? Does certain programs require them?
karan says
Yes, operations like print spooling etc would require the use of singleton pattern.
RAJ says
I have created a child class “trial” and instead of creating objects from parent class, i am creating it from child class and i have created 3 objects.
I think it should have given error by doing this?
Er.chandramohan says
thank you sir but i need another best example for private constructor…i am using a private constructor in another class i can access a private constructor in main class
Hoan Nguyen says
Dear guys,
could you please explain to me the following code:
” private static SingleTonClass obj=null; ”
Thank you in advance,
Hoan
Hau Nguyen says
private static SingleTonClass obj=null;
↓
1.private -> only this SingleTonClass can use it.
2.static -> Because objectCreationMethod() use this variable “obj”. But, objectCreationMethod() is static method, so “obj” must be static.
3.null ->
– At the first time we call objectCreationMethod(), “obj” is null, then we create new object by this command:
obj= new SingleTonClass(); (*)
– The second time we call objectCreationMethod(), “obj” is NOT null because of above command (*).
So, do not create new object, reuse the object which is created at the first time we called objectCreationMethod().
– The same logic for n time calling objectCreationMethod(). There is one and only one object of SingleTonClass is created at the first time.
This is why it is called SingleTon.
Hope this help.
Thanks,
Hau Nguyen
swetha says
what if we declare more than one constructor as private.I mean parameterised constructor..still we can create more than one instance..please provide some more clarity on private constructor
VallaDanger says
Not thread-safe, violates singleton pattern. You either use Enum or double-check.
Pranav says
i tried to make two objects at the same time, and i didn’t face any issue, objects got created and methods could be called easily … why so ??