ClassLoader mechanism

A, ClassLoader concept

  ClassLoader is used to dynamically load the class files into a virtual machine, and converted into an instance of class java.lang.class, each such instance to represent a java class, we can get the information according to the class Class Example of and () method to create such an object by newInstance example, in addition, ClassLoader is also responsible for the resources required to load Java applications, such as image files and configuration files.

  ClassLoader class is an abstract class. If the binary name given to the class, then the class loader will attempt to locate or generate data constitute a class definition. The general strategy is to convert the name to a file name, and then read the "class file" of that name from the file system. ClassLoader class uses a delegation model to search for classes and resources. Each instance has an associated ClassLoader parent class loader. When you need to find a class or resource, ClassLoader instance will attempt to find himself before the class or resource, will be entrusted the task to search for classes or resource to its parent class loader.  

  Note: The program at startup, and will not load all class files to use one-time program, but the program required by Java class loading mechanism to dynamically load a class file into memory.

Two, JVM platform offers three classLoader

  1. Bootstrap classLoader: implemented using native code, is part of the JVM, the JVM loads classes mainly their operational needs, such as java.lang *, java.uti * and the like; These classes are $ JAVA_HOME / jre / lib / rt.jar... Bootstrap ClassLoader does not inherit from ClassLoader, because it is not a normal Java class, the underlying written by C ++, embedded into the JVM kernel which, when the JVM starts, Bootstrap ClassLoader also start with the responsible After loading the core libraries, and configuration Extension ClassLoader and App ClassLoader class loader.
  2. ExtClassLoader: extension class loader, load located in $ JAVA_HOME / jre / lib / ext directory under the extension jar.
  3. AppClassLoader: system class loader, the parent class is ExtClassLoader, load directory and jar under $ CLASSPATH; it is responsible for loading the application's main function category.

  Its architecture is shown below 

  

 

  If you want to implement your own class loader, whether it is to achieve ClassLoader, or inherited abstract class URLClassLoader row, its parent loader is AppClassLoader, because no matter what the parent class loader calls, object creation must eventually call getSystemClassLoader () as father loader, getSystemClassLoader () method to get to exactly what AppClassLoader.

  Note: Bootstrap classLoader JVM does not belong to the hierarchical levels, it does not comply with the rules of ClassLoader loaded, Bootstrap classLoader and no subclasses.

Three , JVM class file is loaded into memory in two ways

  1. Implicit Load: not in code by calling ClassLoader to load the class needs, but by the JVM class automatically loaded into memory required, for example: when a class inherits or reference to a class, the current JVM in this class is not resolved when the memory, these classes are automatically loaded into memory.
  2. Display Load: to load the code in a class by ClassLoader class, such as calling this.getClass.getClassLoader () loadClass () or Class.forName ()..

Fourth, the process ClassLoader loaded classes

  1. Find the .class file and load the file into memory
  2. Link bytecode verification, Class class data structure analysis, and the symbol memory allocation table
  3. Executive class and initialize static properties and static code assignment block

 

Five execution order, class

  • Means that the class is instantiated: creation of instances of a class (object);
  • 类的初始化是指:为类中各个类成员(被static修饰的成员变量)赋初始值的过程,是类生命周期中的一个阶段。

     

 

 

 

 参考:

  https://blog.csdn.net/u014634338/article/details/81434327 

 

Guess you like

Origin www.cnblogs.com/timetellu/p/11615578.html