Defined
The Java reflection mechanism is in the running state, for any class, can know all the properties and methods of this class, for any object, can invoke its arbitrary methods and properties; This dynamic acquisition of information and the ability to dynamically invoke object methods is called the reflection mechanism of the Java language. --Baidu Encyclopedia
Principle
What is a class object
Each Java class corresponds to a class object, and the program compilation stage class object is transformed into a bytecode file stored under the Classpath (the. class file), and the JVM creates the class object for the first time the Java class is loaded. Class objects contain all the information about the class, including variables, methods, and so on.
How the class is loaded
When the JVM starts, it does not load all the other classes into memory at once, in fact the load time of the class is loaded when the class is first used. The approximate loading process when using a class is as follows:
Spatial allocation of classes: refers to the space allocated to static constant members
Class initialization: Initialization of static constant members and execution of static code blocks, in the case of superclass, class first
1 Packagetestrefection;2 3 Importorg.junit.Test;4 5 Public classListtest {6 7 @Test8 Public voidTesthelloworld () {9 Try {TenClass.forName ("Testrefection.testclass1"); OneClass clazz = TestClass3.class; A NewTestClass2 (); -}Catch(ClassNotFoundException e) { - //TODO auto-generated Catch block the e.printstacktrace (); - } - } - + } - + classTestClass1 { A Static { atSystem.out.println ("=================testclass1================"); - } - } - - classTestClass2 { - Static { inSystem.out.println ("=================testclass2================"); - } to } + - classTESTCLASS3 { the Static { *System.out.println ("=================testclass3================"); $ }Panax Notoginseng } - the-------------------------Execution Results------------------------- +=================testclass1================ A=================testclass2================
validation code for class initialization
Gets the class object
In order to perform some operations on an existing class or object by reflection in the process of running the program, you need to get the class object, there are three ways to get the class object:
-
-
- Class.forName (String className); Triggers initialization of the class
- Class name///execute to create class object only, does not trigger space allocation and initialization of classes
- The name of the object. GetClass ()//initialization of classes completed when the object was first created
Support for reflection in class and Java.lang.reflection class libraries
In Java, the class class is fully supported with the Java.lang.reflect Class library for reflection technology. The constructor class in the reflection library is used to manipulate the constructor method, the method class is used for operation methods, and the field class is used for manipulating variables. Its specific use can be found in the next chapter.
Use of reflection technology
About the specific use of reflection technology, many books and articles have been described in detail, the following reproduced a reflection of the use of Technology articles, reprinted address:
Http://www.cnblogs.com/zhaoyanjun/p/6074887.html
The principle and use of Java reflection