I. Loading of classes
When a program wants to use a class, if the class is not yet loaded into memory, the system initializes the class by loading, connecting, and initializing three steps.
Load: means to read the class file into memory and create a class object for it. When any class is used, the system creates a class object.
Connection: 1) Verify that the correct internal structure is in place and that it is consistent with other classes 2) is prepared to allocate memory for static members of the class and set the default initialization value of 3) to resolve to replace the symbolic reference in the binary data of the class with a direct reference.
Initialization: It is the initialization of properties, the construction of initialization, and so on.
Second, the initialization time of the class
1. Creating an instance of a class
2. Access static variables for a class, or assign a value to a static variable
3. Accessing static methods of a class
4. Use reflection to enhance the class object that corresponds to the creation of one of the classes or interfaces
5. Initialize subclasses of a class
6. Use the Java.exe command directly to run a Zhu Lei
Third, class loader
Responsible for loading the. class file into memory and generating the corresponding class object for it.
Although we do not need the loading mechanism of relationship classes, we understand this mechanism and we can better understand the operation of the program.
The composition of the class loader:
Root ClassLoader: BootStrap ClassLoader, also known as the Boot class loader, is responsible for loading Java core classes, such as system, string, and so on. In the JDK Rt.jar file in the Lib directory of the JRE.
Extension ClassLoader: Extension ClassLoader, responsible for loading the jar packages in the extended directory of the JRE. The Ext directory in the Lib directory of the JRE in the JDK.
The system ClassLoader: Systems ClassLoader, which loads the class file from the Java command when the JVM is started, and the jar package and path specified by the CLASSPATH environment variable.
By the above description, we know who the loading of our common things is done by who, so far, we have known to load the class file into memory, then, if we just stand in the view of these class files, if we are to use the contents of these class files? That's the reflection.
Iv. Reflection
The Java reflection mechanism is in the running state, for any class, can know all the properties and methods of this class, for any one object, can call any of its methods and properties; This dynamically acquired information and the ability to dynamically invoke the object's methods are known as the reflection mechanism of the Java language.
To dissect a class, you must first obtain the bytecode file object for that class. The anatomy uses the method in class, so we first get the object of class type for each bytecode file.
Three ways to get a class file object by reflection
package cn7;/** * Reflection: It is through the class file object, to use the member variables in the file, construction methods, member methods. * * to use this, first you have to get the class file tinned, in fact, to get the class object. * class class: * member variables Field * Structuring methods Constructor * member Methods Method * How to get the class file object: The GetClass () method of the * 1.object class * 2. Static property Class * 3 of the data type. Static methods in class classes forname (String classname) * * generally we use the third type in development because it passes in a string instead of a specific class name. This allows us to configure such a string in the configuration file. */publicclass reflectdemo { publicstaticvoid main (String[] args) throws exception { //mode 1 person p = new person (); class<? extends person> c = p.getclass (); &nbSp; person p2 = new person (); class<? extends person> c2 = p2.getclass (); system.out.println (p ==&NBSP;P2);//false system.out.println (C==C2);//true //Way 2 Class<? extends Person> c3 = Person.class; system.out.println (C2==C3);//true //Way 3 class<?> c4 = class.forname ("Cn7. Person "); system.out. println (C&NBSP;==&NBSP;C4);//true }}
This article is from the "11831428" blog, please be sure to keep this source http://11841428.blog.51cto.com/11831428/1877463
The reflection of Java