Most novice programmers are not aware of the possibility to have private constructor in Java. Private constructor is special constructor which is commonly used in classes containing static members. In classes which have private constructor and no public constructor, other classes won’t create any instance in this class. When methods are declared as a private constructor it can be accessed only from within the class. Private constructors are only suitable for internally created objects. Private constructors are widely used in Java Development Kit.
There are the common cases which utilizes Private constructor.
- The Classes which contains only static utility method.
- Classes which contains only constants
- Enum Types
- Singleton Design Pattern
All these examples comes under two types,
- Construction of an object is forbidden entirely
- Only Private object construction is allowed
Construction of object is forbidden entirely:
Construction of objects is prohibited by both caller and the native classes. This is possible only when classes are offered with static members. In this case the absence of “accessible constructor” prevents classes from constructing an object and only the use of static item is permitted. If no constructor to the class is provided by the programmer then a default public constructor will be assigned to the respective class. To prevent or muzzle the public constructor; a private constructor will be assigned. The assigned private constructor is either has no value or an argument private constructor is added to nullify the effect of private constructor.
Only Private object construction is allowed:
Sometimes class will prevent the caller from creating objects so objects will be constructed only internally. In design pattern of Singleton existence of only one object is allowed, which prohibits the idea of creating multiple objects.
In Enum type of JDK 1.4, creation of more than one object is allowed but there is a limitation for using multiple objects.
The program given below is an example for how private constructor is used in singleton class:
package students3k.com;
public class FirstClass
{
//Reference of Static Class
private static FirstClass Obj1=null;
private FirstClass()
{
//Process of private constructor
}
public static FirstClass objMethod()
{
if(Obj1==null)
{
Obj1= new FirstClass();
}
return Obj1;
}
public void display()
{
System.out.println(“The First class Sample code:”);
}
public static void main(String args[])
{
//Particular object may not be developed directly because of the private constructor
//So on it’s way, This object can be created using forced stuff
FirstClass Obj2= FirstClass.objMethod();
Obj2.display();
}
}
The output for the above given example will be: First Class Sample code
What is the use of declaring Private Constructors?
A java programmer can utilize two functions using a private constructor
- Relationship is not possible with existing constructor which means a programmer cannot create any object of class with any other class using default constructor.
- The class present in private constructor is not fit for inheritance which means a programmer cannot extend the class
Relationship is not possible can be explained using the following code
{
private Testing()
{
System.out.println(“The default value of a private constructor is here”);
}
}
public class S3k
{
public static void main(String args[])
{
Testing Test1 = new Testing();
}
}
In the above program the demo class cannot create a the Testing in main() method. The above process will result in compiler error message unless the object declared using a private constructor.
Not fit for inheritance case stated that a class with private constructor cannot be extended which can be explained by the program given below,
{
private Testing()
{
System.out.println(“The default value of a private constructor is here”);
}
}
public class S3k extends Testing
{
public static void main(String args[])
{
S3k Data1 = new S3k();
}
}
The above given program will result in a compiler error because S3k() is a subclass which in turn calls a super class Testing() indirectly but the class Testing() has only private access which cannot be accessed by other classes.