In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
It is believed that many inexperienced people have no idea about how to implement a class loader in java. Therefore, this paper summarizes the causes and solutions of the problem. Through this article, I hope you can solve this problem.
Class class files can new instance objects only after they are loaded through a class loader, so objects and classes in different class loaders are different (even if the class files are the same).
The class loader in java mainly includes boot class loader, extended class loader, system class loader and custom class loader, and each class loader is combined according to the tree level (also known as parent delegation). The boot class loader is responsible for loading the class library in the lib directory and the class loader cannot be directly referenced by the java program; the extended class loader is responsible for loading the class library in the ext directory but can be directly referenced by the java program, and all the class loaders that can be directly referenced by the java program inherit from the CLassLoader abstract class; the system class loader is responsible for loading the class library specified by classpath; and the custom class loader is responsible for loading the class library with the path specified by the user.
Each class loader (except the bootstrap class loader) is designed in a combined mode, so it is generally necessary to specify a parent class loader when customizing the class loader. The advantage of this design is to avoid repeated loading of the same class library to save resources, and when loading a class file, if the parent loader has already loaded the same class file, it will not load the same class file repeatedly (the class file is uniquely identified by the package name and the class name. If not, when the user writes a java.lang.String class, the program already written will be confused. And the user can randomly write a class file that is the same as the system class for malicious destruction.
The following code verifies the above:
Public class ClassLoaderTest {
Public static void main (String [] args) {
/ / TODO Auto-generated method stub
System.out.println (ClassLoaderTest.class.getClassLoader ()); / / 1
System.out.println (ClassLoader.getSystemClassLoader ()); / / 2
System.out.println (ClassLoaderTest.class.getClassLoader () .getParent ()) / / 3
System.out.println (ClassLoaderTest.class.getClassLoader (). GetParent (). GetParent ()); / / 4
}
}
Lines 1 and 2 represent the system class loader, line 3 represents the extension class loader is also the parent of the system class loader, line 4 is that we try to get the parent of the extension class loader, that is, the boot class loader, but the return result is empty (confirming that the boot class loader cannot be directly referenced by the java program).
After reading the above, have you mastered how to implement a classloader in java? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel, thank you for reading!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.