Java class loader and java class loading

Source: Internet
Author: User

Java class loader and java class loading
Class loading process

JVM divides the class loading process into three steps

The following three steps are involved:

JVM divides the class loading process into three steps:Load (Load), Link and initialization)The link is divided into three steps, as shown in:


1) load: Find and load binary data of the class;

2) link:

  • Verify: ensure the correctness of the loaded class;
  • Preparation: allocate memory for static variables of the class and initialize it as the default value;
  • Resolution: converts a symbolic reference in a class to a direct reference;

3) initialization: Assign the correct initial value to the static variables of the class;

So why do I need to verify this step? First, if the class file generated by the compiler is in line with the JVM byte format, but if a master writes a class file by himself, let the JVM load and run it for malicious purposes, this is not good. Therefore, this class file must be verified first. If it does not meet the requirements, it will not be executed again. It is also for security consideration.

The preparation and initialization phases seem to be a bit shield, but they are not shield. If there is a statement in the class: private static int a = 10, the execution process is like this, first, after the bytecode file is loaded to the memory, perform the link verification step first. After the verification is passed, the preparation phase will allocate memory to a because the variable a is static, so at this time, a is equal to the default value of int Type 0, that is, a = 0, and then to the parsing (as described later), to the initialization of this step, to assign the true value of a to a. At this time, a = 10.


Class initialization

Class initialization:

  • 1) create a class instance, that is, a new object
  • 2) access a static variable of a class or interface, or assign a value to the static variable
  • 3) Call static methods of Classes
  • 4) Reflection (Class. forName ("com. lyj. load "))
  • 5) initialize a subclass of a class (the parent class of the subclass will be initialized first)
  • 6) The JVM startup class, that is, the class with the same file name and class name.

Only these 6 conditions will lead to class initialization.


Class Loading

When starting JVM, you can use three class loaders: bootstarp class loaders, extensions class loaders, and application class loaders.


Bootstrap loader: It is only responsible for loading core Java libraries, such as vm. jar and core. jar in the <JAVA_HOME>/jre/lib directory. This class loader is the core part of JVM and is written in native code.

Extension Class Loader: responsible for loading the code in the extension path, generally located in <JAVA_HOME>/jre/lib/ext or through java. ext. code in the path specified by the system property dirs. This class loader is implemented by sun. misc. Launcher $ ExtClassLoader.

Application Class Loader: Generally, Java application classes are loaded by them. You can obtain it through ClassLoader. getSystemClassLoader.


If the application Class Loader needs to load a class, it first delegates the extension class loader, And the extension class loader then delegates the bootstrap class loader. If the parent class loader cannot load the class, the subclass loader searches for the class in its own library. Based on this feature, the class loader is only responsible for classes that cannot be loaded by its ancestor.

Summary:

The class loader is an innovation of java. Basically, all class loaders are an instance loaded with java. ClassLoaderler. The basic purpose is to help us understand the Object Instantiation process and object calling.

 

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.