This question already has an answer here:
My first question is -
class Explain() {
public Explain() {
}
}
Should Constructor always declared as public?
What if I create a private
constructor.
I always seen constructors are implicitly public
. So why private
constructor is useful? Or is it not useful at all. Because nobody could ever call it, or never make an object(because of the private
constructor) ! And that is my second question.
No,Constructors can use any access modifier, including private. (A private constructor means only code within the class itself can instantiate an object of that type, so if the private constructor class wants to allow an instance of the class to be used, the class must provide a static method or variable that allows access to an instance created from within the class.)
Example
Output of above program will be
alpha subsub
No, Constructors can be
public
,private
,protected
ordefault
(no access modifier at all).Making something
private
doesn't mean nobody can access it. It just means that nobody outside the class can access it. Soprivate
constructor is useful too.One of the use of
private
constructor is to serve singleton classes. A singleton class is one which limits the number of objects creation to one. Usingprivate
constructor we can ensure that no more than one object can be created at a time.Example -
More information about access modifiers.
Constructors can have all kind of access modifiers. The usage of different access modifier on constructors are different.
You make a constructor
public
if you want the class to be instantiated from any where.You make a constructor
protected
if you want the class to be inherited and its inherited classes be instantiated.You make a constructor
private
if you want the class to be instantiated just from its own members usually a static block or static method. It means that you take control of instantiating the class and apply some rule on instantiation. Example of usage of private constructor is singleton design pattern.Yes , Constructors can have any access specifier/access modifier.
Private constructors are useful for creating
singleton
classes.Singleton - A singleton class is a class where only a single object can be created at runtime (per JVM) .
A simple example of a singleton class is -
Note, for the above class, the only way to get an object (outside this class) is to call the getInstance() function, which would only create a single instance and keep returning that.
Also, note that this is not thread-safe.
Constructors could be public, default or private and it all depends on what you want to do with it.
For example, if you are defining a Singleton class, you'd better hide (meaning making it private so that it is only available to the class where it belongs) the constructor to prevent other classes to instantiate your class at their will.
You may want to declare it default, let's say, for testing purposes so that test cases within the same package could access it.
More detailed info could be found here