This article brings you a detailed introduction to the Java class loading process. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.
The process of Java files from encoding to final execution:
- Compile: Javac Compile the Java file into a .class file
- Run: Submit the .class file Run the
class loading process for the JVM
The JVM virtual machine loads the class information in the .class file into the memory and parses it to generate the corresponding class object. The JVM does not load all classes into memory from the beginning, but only loads them when it encounters a class that needs to be run for the first time, and only once.
Loading
Load class bytecode files from various sources into memory through the class loader
JVM Three things to accomplish
- Get the binary byte stream that defines this class through the fully qualified name of a class.
- Static storage structure-> Runtime data structure in the method area
- Generate the java.lang.Class object of the corresponding class in the Java heap as the access entrance to these data in the method area.
Class loader
- Start class loader
- Extend class loader
- Apply class loader Device
- Custom class loader
Connection
The process of merging the binary code of the java class into the running state of the JVM
Verification
Ensure that the loaded byte stream complies with the virtual machine specifications and will not cause security errors
Verification classification
- Verification of the file format, such as whether the constant Are there unsupported constants? Is there any non-standard or additional information in the file?
- Verification of metadata, for example, does the class inherit a class modified by final? Do the fields and methods in the class conflict with the parent class? Is there an unreasonable overload?
- Verification of bytecode ensures the rationality of program semantics, such as ensuring the rationality of type conversion.
- Verification of symbol references, such as verifying whether the corresponding class can be found through the fully qualified name in the symbol reference? Verify the accessibility (private, public, etc.) in the symbol reference is accessible by the current class?
Preparation
Allocate memory for class variables (note, not instance variables) and assign initial values (the default initial values of the Java virtual machine according to different variable types)
Default initial value
- The initial value of 8 basic types, the default is 0
- The initial value of the reference type is null
- The initial value of the constant is For the value set in the code
- final static tmp = 456
Resolution
The process of replacing symbol references in the constant pool with direct references
Symbol reference: a string, but this string gives some relevant information that can uniquely identify a method, a variable, or a class
Direct reference: can be understood as a memory address, or an offset Quantity
For example, now call the method hello(), the address of this method is 1234567, then hello is a symbolic reference, 1234567 is a direct reference
In the parsing phase, the virtual machine will The symbolic references of class names, method names, and field names are replaced with specific memory addresses or offsets, that is, direct references
Initialization
to class variables (variables or statements modified by static ) Initialization is the process of executing the class constructor
Initialization process
- When initializing a class, its parent class has not been initialized yet, its parent class will be initialized first
- At the same time Contains multiple static variables and static code blocks, they are executed in top-down order
The above is the detailed content of Detailed introduction to Java class loading process. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.