Advantages of the Java ClassLoader class delegation model?

As I know, ClassLoader first checks to see if the class has been loaded, if not, immediately delegate this check to its parent ClassLoader. But at the end, all classes are loaded by Bootstrap. why do we delegate and what benefits do we achieve by this?

+4
source share
2 answers

An implementation does not always have to be delegated to the parent class loader. It depends on the requirement. If the requirements require that the class be loaded by the implementation of the class loader, it can load it even if it is already loaded by another class loader.

( ), ClassLoader_1, , ClassLoader_2. .

, ( ). Java Boot Class , . JVM .

/ , . ClassCastException, .

, , ,

,

Thread.getContextClassLoader();

, ,

Object o = ....
o.getClass().getClassLoader();
+3

, Decorator. .

, , , all . , , , , .

+1

All Articles