ClassLoader changes in Java 2
The ClassLoader facility has been improved in Java versions 1.2 and later. Any code written for the old system will work, but the new system can make your life a bit easier.
The new model is a delegation model. A ClassLoader can delegate a request for a class to its parent. The default implementation calls on the parent before trying to load the class itself, but this policy can be changed. At the root of all ClassLoaders is the system ClassLoader, which loads classes the default way -- that is, from the local filesystem.
loadClass method generally tries several things to
load a requested class, and if you write a lot of ClassLoaders, you'll find yourself writing variations on the same, fairly complicated method over and over again.
The default implementation of
loadClass in Java 1.2 embodies the most common approach to finding a class and lets you customize it by overriding the new
findClass method, which
loadClass calls at the appropriate time.
The advantage of this approach is that you probably don't have to override
you only have to override
which is less work.
This new method is called by the default implementation of
The purpose of
findClass is to contain all your specialized code for
your ClassLoader, without having to duplicate the other code (such as
calling the system ClassLoader when your specialized method has
Whether you override
getSystemClassLoader gives you direct access
to the system ClassLoader in the form of an actual
ClassLoader object (instead of accessing it implicitly through the
This new method allows a ClassLoader to get at its parent ClassLoader, in order to delegate class requests to it. You might use this approach when your custom ClassLoader can't find a class using your specialized method.
The parent of a ClassLoader is defined as the ClassLoader of the object containing the code that created that ClassLoader.