Related to recommend
Java Basics · Collections
Explain exceptions in Java
15.1 Class loading Overview
When a program wants to use a class, if the class has not been loaded into memory, the system will load, connect, initialize the class to achieve three steps.
1. Load: read the class file into memory and create a class object for it. The system creates a Class object whenever any Class is used.
2. The connection:
(1) Verify whether there is a correct internal structure, and coordinate with other classes.
(2) Preparation is responsible for allocating memory for static members of the class and setting default initialization values.
(3) Parsing replaces symbolic references in the binary data of the class with direct.
3. Initialization: This is the initialization step we discussed before.
15.2 Class loading time
When a Java program first uses a class or interface in any of the following six ways, the system initializes the class or interface.
1. Create an instance of the class.
2. Access or assign a value to a static variable of the class.
3. Call the static method of the class.
4. Initialize a subclass of a class.
5. Run a main class directly using the java.exe command.
6. Use reflection to force the creation of a java.lang.Class object corresponding to a Class or interface.
15.3 Overview and classification of class loaders
1. The classloader is responsible for loading the. Class file into memory and generating the corresponding class entry for it. In Java, the classloader loads a class into the JVM using the following steps:
(1) Load: find and import the Class file;
(2) link: merge binary data of the class into JRE;
-
Check: check the correctness of the loaded Class file data;
-
② Preparation: allocate storage space for static variables of class;
-
(3) Translate symbolic reference into direct reference;
(3) Initialization: perform initialization operations on static variables and static code blocks of the class
(4) When the JVM starts, an initial class loader hierarchy of three loaders is formed
- ①Bootstrap ClassLoader root ClassLoader
- ②Extension ClassLoader
- ③System ClassLoader
2. The role of class loaders
A Bootrap ClassLoader is called a bootloader (also known as a raw or trailing) ClassLoader. It is responsible for loading Java’s core classes. The ClassLoader is not a subclass of java.lang.ClassLoader, but is implemented by the JVM itself. Rt.jar file in the lib directory of the JRE in the JDK.
The Extension ClassLoader is responsible for loading the JAR classes in the JRE Extension directory. Its parent ClassLoader is the same as the ClassLoader. Ext directory in the LIB directory of the JRE in the JDK.
The System ClassLoader is responsible for loading the -classpath option from Java commands, java.class,path System properties, or jar packages and class history specified by the classpath at JVM startup. The system can access the system ClassLoader through a static method of the ClassLoader. If not specified, all user-defined class loaders have class loaders as parent loaders.
15.5 Reflection Overview
JAVA reflection mechanism is in the running state, for any class, can know all the attributes and methods of the class, at the same time for any object, can call any of its methods and attributes, this dynamic information and dynamic call object method function is called the Reflection mechanism of JAVA language.
1. To dissect a Class, it is necessary to obtain the bytecode file object of the Class first, and the method in the Class Class is used for dissect. Therefore, it is necessary to obtain the object of Class type corresponding to each bytecode file first.
2. After each Class is loaded, the system generates a corresponding Class object for that Class, which can be accessed in the JVM. There are three common ways to obtain a Class object
- (1) Use the Class forName(String clazz) static method.
- (2) Call the class attribute of a class to get the class object of that class.
- (3) Call getClass() on an Object, which is a method of the Object class.
15.6 Get and use the constructor with and without parameters by reflection
1. Participation without participation:
2. The output:
3.Constructor:
The newInstance() method of the Class creates objects using the Class’s parameterless constructor. If a class does not have a parameterless constructor, it cannot be created this way. You can call the Class’s getConstructor(String.class,int.class) method to get a specified constructor. The object is then created by calling the newInstance(” triplet “,20) method of the Constructor class.
15.7 Use Reflection to get member variables
The.class.getField (String) method retrieves the specified field (visible public) from the Class.
2. If it is private, it can be obtained by using getDeclaedField(“name”).
3. Use the set(obj, “li Si “) method to set the value of this field on a specified object.
4. If the object is private, set access by calling setAccessible(true). Get (obj) is used to obtain the value of the specified field.
15.8 Obtain methods by reflection and use them
1.Class.getMethod(String,Class…)
And Class. GetDeclaredMethod (String, Class…). Method can get a specified method in a class.
2. Invoke (Object, Object…) You can call this method.
3. Private methods
summary
1. Class loader:
- Process: validation, preparation, parsing.
- Function: Loads a class.
2, reflection fields and methods:
- Reflection field: getField.
- Reflection methods: getMethod, getDeclaredMethod.
Tutorial] [Java based set (link: pan.baidu.com/s/1FLsdFdBg… Extraction code: Java)