Java class loading and class reflection Review

Source: Internet
Author: User

I learned Spring today and suddenly wanted to review the Java class loading and class reflection. Consolidate the underlying principles. Some of them refer to Teacher Li Gang's crazy Java handout and Spring3.x enterprise application development practices by Chen xionghua and Lin kaixiong. 1. A command corresponds to a process. When we start a Java program, that is, when we start a main method, a Java Virtual Machine Process will be started, no matter how complicated the process is. Different JVM processes do not affect each other. That is why Java has only one entry-main method for virtual machines to call. The two mian Methods correspond to two JVM processes. They start two different class loaders and operate on different classes. Therefore, they will not affect each other. Ii. class loading. When we use a class, if the class has not been loaded into the memory, the system will initialize the class by loading, connecting, and initializing. 1. class loading: Read the Class file of the class into JVM and create a Class object for it. 2. Class connection: combines binary data of the Class into the JRE. This is divided into three phases: a). Check: Check whether the data loaded into the Class file is correct. B) preparation: allocate storage space to static variables of the class and perform default initialization. C) parsing: Replace the symbolic reference in the binary data of the class with a direct reference. 3. Initialization: Initialize static variables and static initialization blocks of the class. (Note: If a static attribute of the final type has been obtained during compilation, the class initialization will not be triggered when this attribute is called, because this is equivalent to using a constant; ClassLoader () method, but the class is loaded and not initialized .) Iii. class loaders. The class loader is responsible. the class file is loaded into the memory and the corresponding java is generated for it. lang. class object, which is responsible for loading all classes. Once a Class is added to the JVM, it will not be loaded again. In Java, a class uses its fully qualified class name (package name + class name) as its identifier. In JVM, a class uses its fully qualified class name and its class loader as its identifier. The JVM runtime generates three ClassLoader types: BootstrapClassLoader (root class loader), ExtClassLoader (extension class loader), and AppClassLoader (system class loader ). The UML structure is as follows: 20131206102134359, BootstrapClassLoader is responsible for loading the core class library of JRE. It is not a subclass of ClassLoader and is written in C ++, so we cannot see it in Java, when it is obtained using the getParent () method of its subclass, null is returned. BootstrapClassLoader is responsible for loading rt. jar, charsets. jar, and other Java core class libraries under the JRE target. ExtClassLoader and AppClassLoader are subclasses of ClassLoader. They are not seen in the API, they are located in the rt. jar file. The fully qualified class names are sun. misc. launcher $ ExtClassLoader and sun. misc. launcher $ AppClassLoader. extClassLoader is responsible for loading the JAR package in ext In the JRE extension directory, while AppClassLoader is responsible for loading the class package in the Classpath path. The test is as follows: copy the code package com. stopTalking. crazy; public class TestClassLoader {public static void main (String [] args) {// gets the class loader ClassLoader loader = Thread of the current Thread. currentThread (). getContextClassLoader (); // obtain the class loader of the System class. ClassLoader loader1 = System. class. getClassLoader (); // obtain the class loader of this class TestClassLoader loader2 ClassLoader loader2 = TestClassLoader. class. getClassLoader (); // obtain loader2's parent class ClassLoader loader3 = l Oader2.getParent (); // obtain the parent class ClassLoader loader4 = loader3.getParent (); System. out. println (loader); System. out. println (loader1); System. out. println (loader2); System. out. println (loader3); System. out. println (loader4) ;}} copy the code console output: copy the code // The Class Loader obtained by the current Thread class is AppClassLoadersun. misc. launcher $ AppClassLoader @ 6b97fd // The System class is loaded by the root loader and cannot be accessed in java. Therefore, nullnull // The Class Loader of this class is also AppClassLoadersun. misc. launcher $ AppCl AssLoader@6b97fdsun.misc.Launcher $ ExtClassLoader @ 1c78e57null copy Code 4. class loading mechanism. The JVM Class loading mechanism mainly includes the following three mechanisms: 1. Full responsibility: When a Class loader is responsible for loading a Class, other classes dependent on and referenced by this Class will also be loaded by the Class loader. 2. parent class delegation: first, the parent loader is entrusted to try to load the class. Only when the parent loader cannot load the class can it find and load the class from its own class path. 3. cache mechanism: the cache mechanism ensures that all classes loaded by the home will be cached. When a Class is required in the program, the Class Loader first searches the cache. If no binary data is found in the cache, the system will re-read the binary data corresponding to the Class and create the corresponding Class object. (Note: the parent-child relationship between class loaders is not the parent-child relationship of class inheritance, but the relationship between class loaders instances.) 5. reflection the following is a simple reflection usage. 1. ClassLoader object. You can obtain the current ClassLoader object in two ways, one is through the current Thread: ClassLoader loader = Thread. currentThread (). getContextClassLoader (); If a Class Object already exists, you can use the getClassLoader () method of the Class Object: ClassLoader loader2 = Xxx. class. getClassLoader (); 2. Class Object. There are three methods to obtain the Class Object: a), The forName method of the Class, passed in the fully qualified Class name. Exception ClassNotFoundException B). Call the class attribute of this class. C). Call the getClass () method of an object. 3. Create an object using a Class Object: for example, there is an Apple Class. Class clazz = Class. forName ("com. stopTalking. crazy. apple); this Apple class has a default constructor, a String type parameter constructor, a String type, int Type constructor. There are two ways to obtain an Apple Object: a) use the default constructor of the Apple class: Object o1 = clazz. newInstance (); B), constructor with a String type parameter: Object 02 = clazz. getConstructor (String. class ). newInstance ("apple"); c), with a String type, int Type constructor: Object 02 = clazz. getConstructor (String. class, int. class ). newInstance ("apple", "3"); 4. Call Method: Method m = clazz. getMethod (Method name, parameter type); // create a Method object based on the Method name and parameter type. M. invokej (object, attribute value); // call a method based on the object and method parameters. To call the private Method, call setAccessible (true) of the Method object first. 5. Access attribute: Field f = clazz. getDeclaredField (attribute name); // retrieves the Field object f. get (object); // obtain the attribute value based on the object. 6. Operation Array: Object arr = Array. newInstance (String. class, 10); // create a simple String Array. Array. set (arr, 5, "abc"); // assign the Object name = Array to the element whose subscript is 5. get (arr, 5); // get this element

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.