Instantiate object from a Class which contains private constructor

We know that we cannot make any object if the Class contains private constructor but this is not true until a special care is taken to the private constructor. This tutorial shows an example how to create instance using Reflection even if the classes have private constructors. Though if the special care is taken to the private constructor then you will not be able to create an instance.

The below example creates instances from classes java.lang.System and java.lang.Runtime but you will not be able to create any instance from java.lang.Class because it cheks some security permission and it will throw Security exception.

While accessing Class’s private constructor it throws following exception:

java.lang.SecurityException: Can not make a java.lang.Class constructor accessible
    at java.lang.reflect.AccessibleObject.setAccessible0(
    at java.lang.reflect.AccessibleObject.setAccessible(

The above exception occurs due to the following snippets from class:

private static void setAccessible0(AccessibleObject obj, boolean flag)
    throws SecurityException
    if (obj instanceof Constructor && flag == true) {
        Constructor c = (Constructor)obj;
        if (c.getDeclaringClass() == Class.class) {
        throw new SecurityException("Can not make a java.lang.Class" +
                        " constructor accessible");
    obj.override = flag;

The complete code is given below how to instantiate object from a class which has private constructor.

package com.roytuts.constructor.instance;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;

public class InstantiatePrivateConstructor {

	public static void main(String[] args) {
		String systemClassName = "java.lang.System";
		String runtimeClassName = "java.lang.Runtime";
		String classClassName = "java.lang.Class";

		try {
			Class<?> system = Class.forName(systemClassName);
			Constructor<?> sysConstructor = system.getDeclaredConstructor();

			System sysObject = (System) sysConstructor.newInstance();
			Field[] systemFields = sysObject.getClass().getFields();

			System.out.println("Class Name: " + systemClassName);

			System.out.println("sysObject: " + sysObject);
			System.out.println("systemFields length: " + systemFields.length);

			for (int i = 0; i < systemFields.length; i++) {
				System.out.println("Field Name: " + systemFields[i].getName());
				System.out.println("Field Type: " + systemFields[i].getType());


			Class<?> runtime = Class.forName(runtimeClassName);
			Constructor<?> runConstructor = runtime.getDeclaredConstructor();

			Runtime runObject = (Runtime) runConstructor.newInstance();
			Field[] runFields = runObject.getClass().getFields();

			System.out.println("Class Name: " + runtimeClassName);

			System.out.println("runObject: " + runObject);
			System.out.println("runFields length: " + runFields.length);

			for (int i = 0; i < runFields.length; i++) {
				System.out.println("Field Name: " + runFields[i].getName());
				System.out.println("Field Type: " + runFields[i].getType());


			Class<?> clss = Class.forName(classClassName);
			Constructor<?> classConstructor = clss.getDeclaredConstructor();

			System.out.println("Class Name: " + classClassName);

			System.out.println("Now you will get Exception for below line");

		} catch (InstantiationException | IllegalAccessException | SecurityException | NoSuchMethodException
				| IllegalArgumentException | InvocationTargetException | ClassNotFoundException e) {


 The output of the above class after execution:

Class Name: java.lang.System
sysObject: java.lang.System@63961c42
systemFields length: 3
Field Name: in
Field Type: class
Field Name: out
Field Type: class
Field Name: err
Field Type: class

Class Name: java.lang.Runtime
runObject: java.lang.Runtime@1be6f5c3
runFields length: 0

java.lang.NoSuchMethodException: java.lang.Class.<init>()
	at java.base/java.lang.Class.getConstructor0(
	at java.base/java.lang.Class.getDeclaredConstructor(
	at com.roytuts.constructor.instance.InstantiatePrivateConstructor.main(

 That’s all. Thanks for reading.

Leave a Comment