正确的类加载器实现将:
- 检查该类是否已经加载。
- 通常要求父类加载器加载该类
- 尝试在自己的班级路径中查找班级。
ClassLoader.loadClass的默认实现是这样的:
protected synchronized Class<?> loadClass(String name, boolean resolve) { // First, check if this class loader has directly defined the class or if the // JVM has initiated the class load with this class loader. Class<?> result = findLoadedClass(name); if (result == null) { try { // Next, delegate to the parent. result = getParent().loadClass(name); } catch (ClassNotFoundException ex) { // Finally, search locally if the parent could not find the class. result = findClass(ex); } } // As a remnant of J2SE 1.0.2, link the class if a subclass of the class // loader class requested it (the JVM never calls the method, // loadClass(String) passes false, and the protected access modifier prevents // callers from passing true). if (resolve) { resolveClass(result); } return result;}一些类加载器实现将委派给其他非父类加载器(例如,OSGi,取决于包,委派给类加载器的图),而某些类加载器实现会在委派之前在本地类路径中查找类。



