Importance of Private Constructors in Java


     New and young java learners are quite happy with using public constructors. The concept of having private constructors are quite a hazy idea to them as the real case scenarios of using it is not neatly presented to them. This article brings the concept of private constructors live with code examples and real life use cases.

      We already know that to create an instance of a class, a constructor must be called and in real life cases, the constructor of a class is called outside of the said class. Hence naturally, constructors, most of the time, are declared as public. 

       Let us discuss what would be the effect of declaring a constructor as private. It is simple, it cannot be called outside of the class, and hence an object creation is not possible straight way. But this uncomfortable situation can be wisely utilized to have more control on Object Creation Mechanism. There are plenty of real life situations where the application unit, by virtue of its requirements, might have a maximum numbers (say N) of objects allowed. The class that represents this application unit must have control logic in its code so that no one outside of this class can create more than N numbers of object. If the constructor of this class is declared as public, then the control is gone because any application outside this class can just create object as many times as it wants. In this situation when you need more sophisticated control over object creation, declaring constructors as private will come to you handy.

But there must be a way to access the private constructor out of the class and intuitively, that would be the best place to write the control logic. Yes, this can be done via a static method.  

Let us look into the following code

          The code illustrated above represents a class where the maximum no of allowable object is 2. So we need to ensure that no other application can create 3 or more number of objects out of the class PrivConstr.  To have this control, at the very first, the constructor is made private; this ensures that the freedom of object creation out of the class PrivConstr is withheld and then a static method createObject is written to access this private constructor. The logic of control mechanism is executed at createObject method itself.

         A static final data MAX_ALLOWABLE_INSTANCE has been taken into the class which will store the max count (here in this code it is 2) and a static data OBJ_CNT has been taken which will keep track of count of objects already created. This OBJ_CNT will be incremented whenever the PrivConstr class will be instantiated. In the crerateObject method, before instantiating any object by calling the private constructor, it is to be checked whether the OBJ_CNT has reached the MAX_ALLOWABLE_INSTANCE or not. If OBJ_CNT has already reached the MAX_ALLOWABLE_INSTANCE value, then the private constructor will not be called, hence there will be no object creation. Thus the code for limiting object creation for predefined number of times only, resides within the class itself.

         The code above reminds us that to create any object out of PrivConstr class, a constructor cannot be called directly, rather static method createObject must be called which in turn, with its logical stand point can call the private constructor to create an object.

Let us test the class with a DemoPrivConstr class as follows
         Here the PrivConstr class has been instantiated for three times and there are three if blocks to test whether the object creation has succeeded or not. When obj1 is tried to be created, the static data OBJ_CNT has incremented from 0 to 1 and private constructor is called successfully resulting successful creation of obj1. Then when obj2 is tried to be created, as the OBJ_CNT has not reached the  MAX_ALLOWABLE_INSTANCE  value, once again the private constructor has been called to result the successful creation of obj2; but this time the same the static data OBJ_CNT has been incremented to 2 and thus OBJ_CNT has reached the MAX_ALLOWABLE_INSTANCE value. Hence when obj3 is tried to be created, as   OBJ_CNT has already reached the MAX_ALLOWABLE_INSTANCE value, the private constructor will not be called, so there will be no object creation and as per the code, obj3 will be null.

The output of the code looks like this

The output is as expected. It shows that obj1 and obj2 have been successfully created but obj3 was not created as the class PrivConstr was set not to allow three or more objects to be created out of it.
So if you need to have more control on allowing any prefixed numbers of objects only to be created, the private constructor is a handy tool to be used along with a static method for its easy access.
Now in Java, there is a pretty common notion of Singleton classes. Singleton classes are those which allow only one object to be created out of it. Hence Singleton classes can be designed like this; just the value of MAX_ALLOWABLE_INSTANCE has to be changed to 1. 

No comments:
Write comments

Popular Posts