Java class loading mechanism concept

Source: Internet
Author: User

which part of the entire architecture does the class load first? See the Red Circled section.

What is the class loader divided into the process? Five Processes

    • Load
      • Based on the fully qualified name of the class (simply understood as the absolute path of the class, see appendix), locate the specified bytecode file and produce an Java.lang.Class object in memory, stored in the method area.
    • Verify
      • Role: Ensure that the information contained in the bytecode file conforms to the class file format specification and is safe for the virtual machine.
      • The rules have been updated, with roughly four kinds
        • File Format Verification
          • The analysis is based on the binary byte stream.
        • Meta-data validation (can be interpreted as information validation at the class level)
          • Semantic analysis of the meta-data of a class.
        • Bytecode validation (information validation at the method level)
          • The semantic analysis of the method body.
        • Symbol Reference Validation
          • Verify that the corresponding class, method, and field can be found based on the reference.
      • If you confirm that the bytecode file is secure, close most of the validation by-xverify:none.
    • Get ready
      • Allocates memory and initializes the static variables for the class in the method area.
    • Analytical
      • Replace a symbol reference in a constant pool with a direct reference procedure.
    • Initialization
      • The Execute class constructor <client> method,<client> method is a class variable assignment operation in a class that is automatically collected by the compiler and a statement in a static statement block. The <client> method of the parent class is guaranteed to execute first.
who will execute the loading process

The class loader classLoader, which is Java.lang.ClassLoader. The core function is LoadClass

protected synchronizedClass<?> loadclass (String name,Booleanresolve)throwsClassNotFoundException {//first check that the class is not already loadedClass C =Findloadedclass (name); if(c = =NULL) {        Try {            if(Parent! =NULL) {                //if the parent class is not empty, first check the parent class'sc = Parent.loadclass (name,false); } Else {                //Otherwise check the currentc =FINDBOOTSTRAPCLASS0 (name); }        } Catch(ClassNotFoundException e) {//if it is still not found, it triggers the Findclass method of the subclass itself to findc =Findclass (name); }    }    if(Resolve) {resolveclass (c); }    returnC;}

What kinds of loaders do they have?

Two categories, four small kinds: JDK provided by default three kinds, user-defined one.

The default three types are (non-inheritance relationships)
    • Bootstrap ClassLoader starts the ClassLoader, there is no parent class.
      • Native code (c + +) is generally implemented
      • Core Class (<java_home>/jre/lib/rt.jar) for loading virtual machines, including extension class/system ClassLoader
    • Extension ClassLoader Extension class loader, the parent class loader is empty
      • Java implementation, is the inner class of Sun.misc.Launcher in Rt.jar sun.misc.launcher$extclassloader (Miscellaneous)
      • For loading classes in the extension library (<java_home>/jre/lib/ext)
    • System Classloder Parent Class loader ext ClassLoader
      • Java implementation, is the inner class of sun.misc.Launcher in Rt.jar Sun.misc.launcher$appclassloader
      • Classes used in Class_path
A user-defined parent class is the System ClassLoader
    • Inherit from Java.lang.ClassLoader

What if I have a file with the same name in both loaders?

Here is a concept of parental delegation . is when looking for a class, it will be a layer of up-to-the-way query, if the parent class loader has a corresponding class, the class is loaded directly from the parent class.

Such parental delegation is good, But there are scenarios where we want to destroy the parents ' assignment.
    • Scenario One: We want to load the underlying classloader in the top-level ClassLoader
      • You can put the underlying classloader to Thread.setcontextclassloader () in the thread, and then use Thread.getcontextclassloader () in the ClassLoader in the top layer Load a third-party classloader implementation.
    • Scenario Two: Implementing a class-hot deployment
      • A class can only be loaded once by a single classloader, and when it is necessary to implement a code-hot deployment, it is possible to add a custom ClassLoader to load the new class file every time.
    • Scenario Three: Tomcat uses WebappClassLoader to load separately, not load and then delegate the parent loader to load.

Appendix
    • Fully qualified name of class: The absolute path of the class can be understood, the general rule is the package name. External class Name $ internal class name.
      • member Inner class: Package name. External class Name $ internal class name
      • Anonymous inner class: package name. External class name $1 positive integer-sorted by class load order
      • Local inner class: The package name. The outer class name is a positive integer starting at 1 followed by the local class name-where the number part is the order in which the local class appears in the outer category context

Java class loading mechanism concept

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.