Java class initialization conditions
(1) class loading time:
A) lifecycle: Loading, verification, preparation, parsing, initialization, use, and uninstallation.
B) Five determined loading sequence, verification, preparation, initialization, and uninstallation.
C) initialization conditions (with only four conditions ):
I. triggered if the new, getstatic, putstatic, and invokestatic bytecode is not initialized.
Ii. triggered if Initialization is not performed during reflection calls;
Iii. triggered if the parent class is not initialized during class initialization;
Iv. When a VM is started, the main class (including the main method) triggers initialization;
Key points:
(1) When a subclass calls a static field of the parent class, only initialization of the parent class is triggered, and initialization of the subclass is not triggered;
(2) The initialization phase of a New class is not triggered when an array is added;
(3) the class calls the static final constant and does not trigger class initialization.
Special columns:
The initialization of an interface is slightly different from that of a class, and there is no static code block in the interface. The difference is that the parent class is initialized in the third entry of the class initialization condition. If it is an interface, the parent class interface is initialized when the parent class is referenced.
Java class loading process
(1) load:
A) obtain the binary byte stream based on the class permission name;
B) convert the static storage result represented in the byte stream obtained by the above actions into a data structure for running;
C) generate a class object in the java heap as the data access entry.
(2) Verification (confirm that the byte stream information meets the Virtual Machine Requirements ):
A) File Format verification: ensure that the obtained binary byte stream can be correctly parsed and stored in the method area;
B) Metadata verification: conduct semantic analysis to ensure compliance with java language specifications;
C) bytecode verification: mainly for data flow and control flow analysis;
D) symbol reference verification: ensure that the parsing phase runs normally. If the verification fails, an exception is thrown.
(3) Preparation (the phase of allocating memory for the variable and setting the initial value of the class variable)
The initialization part is only a variable, which is usually zero.
If it has final modification, it is initialized to the specified value.
(4) Resolution
The process of converting the symbol reference in the constant pool into a direct reference
A) parsing of classes or interfaces:
I. If it is not an array type, pass the full qualified name to the loader of the current class;
Ii. If it is an array type and is an object element, similar to the Integer class, the element type is loaded according to the first point. If it is an integer class, the dimension heap is directly generated.
Iii. If there is no exception in the above steps, verify the symbolic reference and confirm the access permission.
B) Field parsing:
I. Fields with simple names and field descriptions that match the target are returned;
Ii. Otherwise, if an interface is implemented, perform a recursive search from the top down and find the result;
Iii. Otherwise, perform a recursive search from the top down according to the inheritance relationship and return the result if it is found. Otherwise, an exception is thrown;
C) Class Method Analysis
A little different from field interpretation is that when you look for a method, you first find the parent class instead of the interface. Other logics are returned if matching is found. Check whether it is an abstract class, because the matched method may be the list of Implemented interfaces and the method of the parent interface; otherwise, NOsuchmethod is abnormal.
D) interface method Parsing
The process is the same as that of the class method. The difference is that you do not need to determine whether the method has been implemented.
(5) initialization
This stage begins to execute java code. This stage is the process of executing the constructor.
Java Class Loader
Three types of loaders in Java: start class loaders, extension class loaders, and application class loaders. Recommended two-parent delegation model. Each loading request requires that the load request be sent to the parent class loader for loading. All loading requests will be sent to the start class loader, if the parent classloader returns feedback and cannot complete the load request, it will try to load it by itself.
OSGI is one of the cases that undermine the parent-parent delegation model.