Can a constructor be private? How is a private constructor useful?
Yes.
This is so that you can control how the class is instantiated. If you make the constructor private, and then create a visible constructor method that returns instances of the class, you can do things like limit the number of creations (typically, guarantee there is exactly one instance) or recycle instances or other construction-related tasks.
Doing new x()
never returns null
, but using the factory pattern, you can return null
, or even return different subtypes.
You might use it also for a class which has no instance members or properties, just static ones - as in a utility function class.
Private constructors prevent a class from being explicitly instantiated by callers see further information on PrivateConstructor
Yes, a constructor can be private. There are different uses of this. One such use is for the singleton design anti-pattern, which I would advise against you using. Another, more legitimate use, is in delegating constructors; you can have one constructor that takes lots of different options that is really an implementation detail, so you make it private, but then your remaining constructors delegate to it.
As an example of delegating constructors, the following class allows you to save a value and a type, but it only lets you do it for a subset of types, so making the general constructor private is needed to ensure that only the permitted types are used. The common private constructor helps code reuse.
public class MyClass
{
public MyClass(int x){
this(Integer.toString(x),"int");
}
public MyClass(boolean x){
this(Boolean.toString(x),"boolean");
}
private MyClass(String value, String type){
_value = value;
_type = type;
}
public String toString(){
return _value;
}
public String getType(){
return _type;
}
private String _value;
private String _type;
}
Yes and it is used to prevent instantiation and subsequently overriding. This is most often used in singleton classes.
I expected that someone would've mentioned this (the 2nd point), but.. there are three uses of private constructors:
to prevent instantiation outside of the object, in the following cases:
- singleton
- factory method
- static-methods-only (utility) class
- constants-only class
.
to prevent sublcassing (extending). If you make only a private constructor, no class can extend your class, because it can't call the
super()
constructor. This is some kind of a synonym forfinal
overloaded constructors - as a result of overloading methods and constructors, some may be private and some public. Especially in case when there is a non-public class that you use in your constructors, you may create a public constructor that creates an instance of that class and then passes it to a private constructor.
Private Constructors can be defnied in the Java for the following reasons
To have control on the instantiation of the Java objects, it wont allow you to create an instance of an object.
It wont allow the class to be Subclassed
This has a special advantage when implementing the singleton Pattern, Private contstructors are used for it and have a control on the creating the instance for the whole application.
when you want to have a class with all constants defined and Does not require its instance any more, then we declare that class as a private constructor.
Some reasons where you may need private constructor: The constructor can only be accessed from static factory method inside the class itself. Singleton can also belong to this category. A utility class, that only contains static methods.
Yes.
A private constructor is used to prevent instance initializing, such as the Math final class you use in java. Singleton also use private constructor
Basic idea behind having a private constructor is to restrict the instantiation of a class from outside by JVM, but if a class having a argument constructor, then it infers that one is intentionally instantiating.