private constructor is used when you want this class to not be intitalise from the outside
Using
Case 1: When creating classmates
public class SingletonClass { public static SingletonClass singletonClass; private SingletonClass() { } public static SingletonClass getInstance() { if(singletonClass == null) { singletonClass = new SingletonClass(); } return singletonClass; } }
In this case, only the intialization method is executed by the getInstance method. No one can create a SingletonClass object outside.
Case 2: if you do not want any instance of the object, as in usage classes
public final class Util { private Util() { } }
In the util class, all methods are static, so there is no need to create your own object, therefore, in this case, a private constructor is used
Ashish Aggarwal Jun 27 '13 at 12:14 2013-06-27 12:14
source share