Class loaders are loaded into memory by the ClassLoader. Can I add my own java.lang.String class and write some code secretly to confuse others?

  • The answer is noSecurity issuesDoes not appear because it is present when the JVM loads the classParent delegation mechanism.
  1. Java virtual machines load Class files on demand
  2. That is, when the class is needed, its class files are loaded into memory to generate class objects.
  3. And when loading a class file for a class, the Java virtual machine uses parental delegation mode
  4. That is, the request is handled by the parent class, which is a task delegation pattern.



Working principle:

  1. If a classloader receives a classload request, it does not load the request itself. Instead, it delegates the request to the parent class’s loader.
  2. If the parent class loader also has a parent class loader, delegate further up, recursively. The request will eventually reach the top level of the startup class loader;
  3. If the parent class loader can complete the task, it returns successfully. If the parent class loader cannot complete the task, the child loader will try to load itself. This is the parent delegate mode.

The family bought a star fruit

  1. Grandson Xiaoming gets it first. What do you want? A look at his father, he gave his father, said dad, you eat
  2. Xiao Ming’s father is trying to eat carambola, thought of the child’s grandfather did not eat it, handed it to Xiao Ming’s grandfather, said dad, you eat it.
  3. Xiao Ming grandpa fruit, a look so fresh, reluctant to eat, handed his son, said son, you eat.
  4. Xiao Ming’s father took the star fruit, a look, feel or let Xiao Ming eat good, handed to Xiao Ming.
  5. Xiao Ming fruit star fruit, happy to eat up.

See, star fruit is the class to load as long as dad and Grandpa and anyone wants to eat star fruit, it’s not Ming’s turn.

  1. AppClassLoader takes over the task of loading, and when I see the dad ExtClassLoader, I hand it over to the dad ExtClassLoader.
  2. ExtClassLoader result task, to load, a look at dad BootStrapClassLoader in, handed to dad BootStrapClassLoader.
  3. The BootStrapClassLoader sees that this class is indeed within its scope. It was recorded. So even if a programmer writes a java.lang.String on purpose, it won’t load, because grandpa has to load it first, and load the java.lang.String in his own acre of land. Instead of the programmer-defined java.lang.String, which is not part of the responsibility of bootStrapClassLoader.

Java.lang.StringSSS is a special class. The programmer was ruthless enough to define a class that did not exist.

  1. AppClassLoader takes over the task of loading, and when I see the dad ExtClassLoader, I hand it over to the dad ExtClassLoader.
  2. ExtClassLoader result task, to load, a look at dad BootStrapClassLoader in, handed to dad BootStrapClassLoader.
  3. When BootStrapClassLoader sees this class to load, it first looks around and finds that there is no class, so it loads all the classes to ExtClassLoader and says, “Load it, I don’t have this class here.
  4. When the ExtClassLoader gets the class, it can check to see if it has the class. When it first gets the class, it must give it to its father, and it can’t even look it up. So I looked around and found none, so I gave the loading right to the AppClassLoader
  5. AppClassLoader result permission, a check, sure enough, on the load. That’s how it works. That’s the parent delegate mechanism.

Parent delegate is son -> father -> father; Then the process of father -> father -> son is called parental delegation. Don’t get me wrong. Not another parent.

So what are the responsibilities of the various classloaders that have been achieved? Listen next time!

The hard work of self-discipline is better than the bitter fruit of regret. Focus on Java backend technology and solutions, good at summing up, sharing! The hard work of self-discipline is better than the bitter fruit of regret. Focus on Java backend technology and solutions, good at summing up, sharing! The hard work of self-discipline is better than the bitter fruit of regret. Focus on Java backend technology and solutions, good at summing up, sharing!