Why a private constructor? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 5

Why a private constructor?

In many high-level languages, you can declare a constructor as private. What are reasons for having a private constructor?

5th Jun 2019, 5:48 AM
Paolo De Nictolis
Paolo De Nictolis - avatar
6 Answers
+ 20
https://www.programcreek.com/2011/11/when-would-you-like-a-private-constructor-method-in-java/ If a method is private, it means that it can not be accessed from any class other than itself, you know that. When it is used appropriately, it can produce security and functionality. Constructors, like regular methods, can also be declared as private. When a class needs to prevent the caller from creating objects. Private constructors are suitable. Objects can be constructed only internally. One application is in 'the singleton design pattern'. The policy is that only one object of that class is supposed to exist. So no other class than itself can access the constructor. This ensures the single instance existence of the class. Private constructors have been widely used in JDK.
5th Jun 2019, 7:54 AM
Danijel Ivanović
Danijel Ivanović - avatar
+ 19
HonFu The main idea of it is to ensure that only one single instance of the class could be created at any given time. ( Being so simple however, singleton raised a lot of the discussions about how to make it right... ) • The Utility/Helper Class represents the non-instantiable class (with constructor declared as 'private' ), optionally declared as 'final' and contains 'static' methods only.
5th Jun 2019, 8:56 AM
Danijel Ivanović
Danijel Ivanović - avatar
+ 19
HonFu • Declare a private constructor to prevent others from instantiating the class, you can ensure that a class has only one instance. • Create the instance of the class either during class loading in a static field/block, or on-demand in a static method that first checks whether the instance exists or not and creates a new one only if it doesn’t exist.
5th Jun 2019, 9:25 AM
Danijel Ivanović
Danijel Ivanović - avatar
+ 6
Danijel Ivanović, with a private constructor, how is the first/single instance constructed?
5th Jun 2019, 8:19 AM
HonFu
HonFu - avatar
+ 5
Private or protected? I can see a use case for protected constructors: You can create a pseudo-abstract super class. You inherit from it and delegate to the protected constructors from the constructors of the derived classes. Like that, you can't instantiate a 'mother', but only 'kids'.
5th Jun 2019, 6:09 AM
HonFu
HonFu - avatar
+ 2
example [java] : public class sample{ private sample(){} public static sample getSample(){ return new sample(); } } public class Main{ public static void main(String[] args){ sample s = sample.getSample(); } } ////////////////////////// Most people use this way in design pattern, when your constructor is private, you can not create an object in a normal way.
21st Jul 2019, 7:07 AM
Mohamad.M.Nosrati
Mohamad.M.Nosrati - avatar