Course – LS – All

Get started with Spring 5 and Spring Boot 2, through the Learn Spring course:


1. Overview

In this tutorial, we'll see why we'd use a private constructor for a class in Java and how to use it.

2. Why Use a Private Constructor?

In Java, we can declare a constructor as private using the private access specifier. If a constructor is declared private, we can't create an object of the class, except within the class.

A private constructor is used when we want to limit the way objects of a class are instantiated. For example, we might do this if we want to create an object by only using a factory class that can create those objects. Or another situation is when we want to have only one object instance of that class.

Some of the most used cases of a private constructor are Singleton, Builder,  and Factory, the creational design patterns.

Now, we can imagine any combination of these patterns as they can blend nicely and achieve a robust codebase.

3. Accessing the Private Constructor

Usually, in order to call the private constructor, the use cases listed above have other public methods that would call the private constructor within the class.

Alternatively, we can use the Java Reflection API to directly access the private constructor.

The Java Reflection API is an advanced feature that allows programs to examine and modify the runtime behavior of the application running within the JVM. Because of this, using this method isn't recommended as it can lead to difficulty spotting and fixing bugs.

Using Reflection, we can see the methods and attributes of any class and modify or access them bypassing the access modifiers.

The most used case for using reflection is unit testing a class that has private methods. To unit test a private constructor or method using reflection, we'd need to do the following steps:

  • get the class object for the class that we want to instantiate
  • with the class object, call the getDeclaredContructor() method to get the Constructor object
  • on the Constructor object, call the setAccessible() method and make the constructor accessible
  • after the Constructor object is accessible, we can call the newInstance() method that will create a new object of that class

Let's create a class with a private constructor. Then we'll use the Java Reflection API to instantiate it and make sure that the private constructor was called:

private PrivateConstructorClass() {
    System.out.println("Used the private constructor!");

Let's add a unit test where we instantiate this class using the private constructor:

public void whenConstructorIsPrivate_thenInstanceSuccess() throws Exception {
    Constructor<PrivateConstructorClass> pcc = PrivateConstructorClass.class.getDeclaredConstructor();
    PrivateConstructorClass privateConstructorInstance = pcc.newInstance();
    Assertions.assertTrue(privateConstructorInstance instanceof PrivateConstructorClass);

In the console output, we should see that the private constructor was called and the print inside the constructor displayed the message. In spite of the private access modifier, we can now call the private constructor and instantiate new objects.

4. Conclusion

In this article, we saw why we'd use a private constructor and a few different ways to use it. We also learned that we can create public methods to access a private constructor or use the advanced Java Reflection API for a more advanced approach.

As always, the full implementation of these examples can be found over on GitHub.

Course – LS – All

Get started with Spring 5 and Spring Boot 2, through the Learn Spring course:

res – REST with Spring (eBook) (everywhere)
Comments are closed on this article!