What is parental delegation
When each classloader receives a request to load a class, it delegates the request to its parent loader. When the parent loader cannot load the class, the task is returned to the child loader
The benefits of parental delegation
- Prevent reloading classes
- Security. The Java core API has been tampered with. Suppose you pass a name called
java.lang.Integer
The parent delegate mode is passed to the launcher class loader, which finds the name of the class in the core Java API, finds that the class has been loaded, and does not reload the class passed by the networkjava.lang.Integer
, and returns the loaded integer.class directly, thus preventing the core API library from being tampered with. You might be thinking, if we customize a path named classpathjava.lang.SingleInterge
What about classes (which are made up)? Class does not existjava.lang
In the parent delegate mode, the class is passed to the startup class loader. Since there is no class in the parent class loader path, the class will not be loaded. The reverse delegate will be loaded to the subclass loader, and eventually the class will be loaded through the system class loader. But that’s not allowed becausejava.lang
Is the core API package, requires access permission, forced load will be reported exception