Search code examples
javaclassclassloader

What is the purpose of cachedConstructor field in java.lang.Class?


Is it used by classloader? Can you recommend any specific articles or documentation to read about peculiarities of java.lang.Class and classloaders?


Solution

  • You wont find much in the way of documentation for this because it is a field private to the inner workings of the java.lang.Class and may or may not exist depending on the implementation of the JDK you are looking at.

    If you have a look at the source code for OpenJDK at the newInstance() method I think it is quite obviously what is it doing. It is caching the reference to the default constructor so that repetitive calls to newInstance() don't have to find it each time.

    A simple Google search found a brief article about the internals of the ClassLoader.