Saturday, April 5, 2014

Private constructor in Java

Problem

In terms of inheritance, what is the effect of keeping a constructor private?

Solution


Private constructors prevent a class from being explicitly instantiated by its callers i.e. it cannot be instantiated. Additionally, because the constructor is private, the class also cannot be inherited.

Benefits of this are many :
  • classes containing only static utility methods
  • classes containing only constants
  • type safe enumerations
  • singletons

It is used while implementing the Singleton pattern. The class hides its constructor from outside, therefore maintaining a single copy of itself.

Declaring the constructor private will ensure that no one outside of the class can directly instantiate the class. In this case, the only way to create an instance of the class is by providing a static public method, as is done when using the Factory Method Pattern.
Other references:
Private constructors prevent a class from being explicitly instantiated by callers.
There are some common cases where a private constructor can be useful:
  1. classes containing only static utility methods
  2. classes containing only constants
  3. type safe enumerations
  4. singletons
The above 4 cases, can be grouped into 2 : 
  • Object construction is entirely forbidden. No objects can be constructed, either by the caller or by the native class. This is only suitable for classes that offer only static members to the caller.
  • Object construction is allowed - It is the case of singletons.

Case 1/2/3 - Class containing constants OR static methods OR type safe enums
public final class Consts  {
  public static final boolean PASSES = true;
  public static final boolean FAILS = false;
  public static final boolean SUCCESS = true;
  public static final boolean FAILURE = false;
 
  public static final int NOT_FOUND = -1;
   
  public static final String NEW_LINE = 
    System.getProperty("line.separator");
  public static final String FILE_SEPARATOR = 
    System.getProperty("file.separator");
  public static final String PATH_SEPARATOR = 
    System.getProperty("path.separator");
   
  public static final String EMPTY_STRING = "";
  public static final String SPACE = " ";
  public static final String TAB = "\t";
  public static final String SINGLE_QUOTE = "'";
  public static final String PERIOD = ".";
  public static final String DOUBLE_QUOTE = "\"";
 
  private Consts(){
    //this prevents even the native class from 
    //calling this ctor as well :
    throw new AssertionError();
  }
}

If the programmer does not provide a constructor for a class, then the system will always provide a default, public no-argument constructor. To disable this default constructor, simply add a private no-argument constructor to the class. This private constructor may be empty. Somewhat paradoxically, creation of objects by the caller is in effect disabled by the addition of this private no-argument constructor.

Case 2 - Object construction is private only
Objects can be constructed, but only internally. For some reason, a class needs to prevent the caller from creating objects.
In the case of a singleton, the policy is that only one object of that class is supposed to exist. Creation of multiple objects of that class is forbidden.
public final class Universe {
 
  public static Universe getInstance() {
     return fINSTANCE;
  }
 
  // PRIVATE //
 
  /**
  * Single instance created upon class loading.
  */
  private static final Universe fINSTANCE =  new Universe();
 
  /**
  * Private constructor prevents construction outside this class.
  */
  private Universe() {
    //..elided
  }
}

References
http://tianrunhe.wordpress.com/2012/04/14/private-constructor-in-java/

Reactions:

0 comments:

Post a Comment