Difference between Class.forName() and ClassLoader.loadClass() methods in Java?

java

This is one of the most frequently asked questions in Java interviews. This question describes how deeply you know about class loading, dynamic class loading mechanism and class initialization. If you are interested to know about class loading mechanism in Java, please read my article here. Both Class.forName(String className) and ClassLoader.loadClass(String className) methods try to dynamically load class with given string class name and both methods return java.lang.Class objects for given string class name.

But now question arise that if both methods have same behavior than what is the difference between them? There is one visible difference is that, where Class.forName(String className) method is static method of java.lang.Class class, in other hand, ClassLoader.loadClass(String className) method is instance method (non-static method). Therefore, if you want to use and call ClassLoader.loadClass(String className) method, you need any java.lang.ClassLoader instance to load any class with this method.

One another difference is ClassLoader used by both methods to load given class. Where Class.forName(String className) method use the same ClassLoader to load the class, which is used by it’s caller. For example, if you have created a class XYZ in which you have used Class.forName(String className) method to load any given class, in this case, Class.forName(String className) method will use same ClassLoader with which XYZ class has loaded. Whereas ClassLoader.loadClass(String className) method use that ClassLoader on which ClassLoader instance this method is called. If you are concerned about any specific or your own ClassLoader to load any class then you should use ClassLoader.loadClass(String className) method to load any given class.

Let us discuss one major difference between both methods. The major difference between both methods is class initialization after loading any given class. Class.forName(String className) method load given class dynamically and initialize it statically. It initialize all static fields of this class and it’s super class static fields recursively. Whereas ClassLoader.loadClass(String className) method load the class dynamically but delay the initialization of given class. Therefore, choice of use from both methods depends on situations, if you know, static initialization of any class is costly, then you could choose delay initialization using ClassLoader.loadClass(String className) method.

But there is one more method in java.lang.Class class, in which you can specify your own ClassLoader to load any class and delay class initialization as well. There is one method with three parameters in which you can specify your ClassLoader and set Boolean flag, whether you want to initialize that class or not. For example, Class.forName(String className, boolean initialize, ClassLoader loader). This method can be use for more flexibility and customization like initialization and ClassLoader.

I hope now you are bit more clear about differences in both methods which generally look alike. If you know any more differences please share with us.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>