In the parent delegation model for loading classes, I know that loadclass() is invoked on the parent, all the way to the top of the class-loader hierarchy (assuming the class is not loaded). At which point the topmost parent classloader’s findClass is invoked.
If this class is not found, how is the control transferred to the next classloader’s findClass method ?
In the parent delegation model for loading classes, I know that loadclass() is invoked
Share
The findClass(String) will be invoked by the loadClass(String) method of the classloader. It’s default
implementation throws a ClassNotFoundException and is intended to be overridden by classloaders.
The loadClass(String) method will call the following methods in that order
findLoadedClass(String)loadClass(String)method.findClass(String)method (custom loading)So all a custom classloader has to do is override the
findClass(String)method to load classes in a custom way. This will ensure the proper delegation in classloading. Check the links (javadoc), it explains what steps are taken and how thefindClass(String)is called fromloadClass(String)So the classloading takes place in following order (example)
ClassLoader A with parent B (only explaining the findClass and loadClass)
At any given time if the class is found (eigther by findClass or findLoadedClass), that class is returned.