Search code examples
javajvmclassloader

i think it is enough that only one kind of classloader in java


There are multiple subclasses of ClassLoader: BootstrapClassLoader,ExtClassLoader,AppClassLoader and so on.

However, I think only one BootstrapClassLoader is enough!

Can someone please tell me why there are so many different kinds of ClassLoaders?

NOTICE: this is why there are so many kinds of classloaders, not why there are so many instances of classloader!


Solution

  • Each class loader is designed to load classes from different locations. For instance, you can actually create a class loader that will load a class file from a networked server or download the binary of a class from a remote web server, etc. The logic that performs this operation is baked into the class loader itself and provides a consistent interface so that clients can load classes regardless of how the class loader actually performs the loading. The BootstrapClassLoader is capable of loading classes from the JVM_HOME/lib directory...but what if you need to load them from a different location??

    In short, because there as an infinite (well, not quite) number of ways to load classes and there needs to be a flexible system to allow developers to load them however they want.