Search code examples
javaapacheobjecttomcatclassloader

Why Bootstrap class of Apache tomcat calls ClassLoader.loadclass instead of new operator?


I was going through apache tomcat opensource code, and at the start, in Bootstrap.java i found this:

Class<?> startupClass = catalinaLoader.loadClass("org.apache.catalina.startup.Catalina");

even when Bootstrap.java and this Catalina.java belongs to the same package, Why Bootstrap(org.apache.catalina.startup) class of apache tomcat calls ClassLoader.loadclass to load Catalina class(org.apache.catalina.startup.Catalina) and then class.newInstance, why not it just create Catalina instance using new operator?


Solution

  • Since the code uses explicitly catalinaLoader and assuming it later creates an instance of the class, it's to make sure that the Catalina class is loaded with a different classloader than the Bootstrap class.

    For further information what this means, refer to documentation about class loading.