Private constructor in Java

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

My initial thoughts:
A private constructor is used when we are implementing the Singleton pattern. The class hides its constructor from outside, therefore maintaining a single copy of itself.

Solution:
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. Additionally, because the constructor is private, the class also cannot be inherited.

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:

  • classes containing only static utility methods
  • classes containing only constants
  • type safe enumerations
  • singletons

These examples fall into two categories.

  1. 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.
    In these cases, the lack of an accessbile constructor says to the caller : “There are no use cases for this class where you need to build an object. You can only use static items. I am preventing you from even trying to build an object of this class.” See class for constants for an illustration.

    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.

  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
      }
    }
    
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: