1, trigger the class load reason (active call and passive call):
Six types of active calls:
1), create an instance of the class (new operation, reflection, cloning, deserialization)
2), calling the static method of the class
3), use or assign a value to the static property of the class/interface (excluding static final and constant expressions determined at compile time (including constants, string constants)
4), invoke the reflection method in the API, Class.forName () and so on.
5), subclass is initialized
6), set as Startup class at JVM Startup (main class with Main method)
Others are passive references: Passive references do not trigger initialization of the class (load, link only), such as declaring only a reference to a class, referencing a class through an array definition, and so on.
2. The complete life process of class loading
Load, link (validate, prepare, parse), initialize, use, uninstall
1), loading
i), the Java compiler loads the binary byte stream file (. class file) of the class, and if the class has a base class, it is loaded up to the base class (regardless of whether the basic class is used or not).
II), loading the binary bytecode into memory and parsing the data structure corresponding to the method area.
III), generate the Java.lang.Class object of the class in the Java logical heap as a portal to that class in the method area.
Class Loader: Sub-default loader and user custom loader
Bootstrap ClassLoader: Top-level loader, implemented by C + +. Responsible for loading the JDK core class library at the start of the JVM and loading the next two class loaders.
Extension ClassLoader: A class that inherits from ClassLoader and is responsible for loading all jar packages in the {Java_home}/jre/lib/ext directory.
App ClassLoader: The sub-object of the loader above, responsible for loading the class file and jar package in the application Classpath directory.
Customer ClassLoader: The user inherits the custom loader from the ClassLoader class to handle special loading requirements. such as Tomcat have their own implementation of the loader.
The ClassLoader uses a parent delegate (bottom-up query) to avoid repeating load classes, while the load order is top-down.
2), Link
i), validation: Check validation of bytecode integrity, final class and method, method signature, and so on.
II), prepare: Allocate storage space for static variables (memory unit 0, that is, the base type is the default value, the reference type is null).
III), parsing (this step is optional): Replace the symbol reference in the constant pool with a direct reference.
Classes are loaded and linked only once, so static members are only loaded once, shared by all instances of the class, as owned by the class.
3), initialize
Includes initialization of the class, initialization of the object.
Initialization of the class: Initializes a static field (performs the initialization action at the definition), executes the static initialization block.
Initialization of the object:
If you need to create an object, the creation object is executed and initialized:
i), allocate enough storage space on the heap for the created object, and clear the storage unit, that is, the base type is the default value, and the reference type is null.
i), if there is a base class, recursive initialization of non-static member variables (that is, executing the initialization action at the variable definition), the construction method.
II), and finally initialize the non-static members of this class, the construction method.
4), use
5), uninstall
The object's reference (in the stack) is immediately reclaimed by the system after it has gone out of scope, and the object itself (in the heap) is marked as garbage by the GC and recycled the next time the GC performs garbage processing.
Summary: A class First initializes static variables and static blocks;
Then allocates the memory space of the class and the member variables of the parent class, assigns the initial value, and then calls the construction method;
Between the parent and child classes, it is always preferable to create and initialize the parent class.
That is: (static variable, static initialization block) –> (variable, initialization block) –> constructor, where the base class always takes precedence over the subclass.
Reference: Introduction to loading order of classes in Java (ClassLoader)
Java class loading and initialization
Java Series notes (1)-Java class loading and initialization
Summary of loading and initialization of Java classes