Sunday, July 10, 2011

Java Tips: Why a PRIVATE constructor?

A. Introduction:
As a private constructor doesn't allow anyone outside the class to instantiate it, what is the use of that "isolated" class then?

Well, setting the access modifier of a constructor to private is a design decision to enforce noninstantiability of a class and therefore, prevent the misuse of an object's creation by the outside world.

If you don't define any constructors for your class, Java implicitly defines a default constructor, which is public.

Two common cases make use of defining a constructor as PRIVATE:
- The instantiation of a class is forbidden
- The instantiation of a class is done only via a dedicated manager / factory


B. Instantiation of a class is forbidden
In case the instantiation of a class is forbidden, it is recommended to make the private constructur throw an exception. This way, you make sure that even methods within the same class cannot call the constructor.

class DangerousClass {

private DangerousClass() {
throw new AssertionError();
}

}


Examples for this use case:
Utility classes having only static methods.
Click here for an interesting article about the Laws of Utility Classes.

Contsant classes holding only static constants.
Click here for more details about constant classes.


C. Instantiation of a class is done only via a dedicated manager / factory
In this case a public static method inside the class takes over the responsibility of creating the objects. This design practice is needed when certain criteria have to be checked before constructing the object, e.g. input validation, number of existing instances....etc

Examples for this use case:
Enum Types:

The constructor for an enum type must have package-private or private access. It automatically creates the constants that are defined at the beginning of the enum body. You cannot invoke an enum constructor yourself.

For more details about enum types, please refer to the following articles
Enum Types: http://download.oracle.com/javase/tutorial/java/javaOO/enum.html
Type-Safe Enumerations: http://www.javapractices.com/topic/TopicAction.do?Id=1


Singleton design pattern:
The Singleton design pattern ensures that a class has only one instance and provides a global point of access to that instance.
Click here for more details about the Singleton Design Pattern.

I also recommended reading this article "When is a Singleton not a Singleton?". It presents common mistakes when working with Singletons.


D. References and further readings
Private constructor:
This article provides a very nice and organized answer to the same question handled in this article.
http://www.javapractices.com/topic/TopicAction.do?Id=40

What's the point of having a private constructor?
This article belongs to a set of salient interview questions that cover a broad range of topics in Java.
http://www.programmerinterview.com/index.php/java-questions/why-have-a-private-constructor/

2 comments:

  1. Good question. Always been asked by junior developers :)

    I like the way you organize your article and simplify the info.

    Well done!

    ReplyDelete
    Replies
    1. Hi there,

      Thank you very much for your nice comment. I am glad to know that you like the way, I structure the articles and explain the different questions.

      Please feel free to drop a comment if you also see any points that need improvement.

      Have a nice day,
      Samar

      Delete