I. Overview of REFLECTIONS
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 called the reflection mechanisms of the Java language.
The above summary is what is reflected
Reflection is the mapping of various components in Java classes into Java objects.
For example: A class has: member variables, methods, construction methods, packages and so on, using the reflection technology can be a class dissection, each component is mapped into one object.
(In fact, these member methods, constructor methods, and classes in a class are described)
is the normal loading process of a class: the principle of reflection is in relation to the class object.
Be familiar with loading: The Class object is the origin of reading the class file into memory and creating a class object for it.
1, obtain the class object three ways 1.1 object--> getclass ();
1.2 Any data type (including basic data types) has a "Static" Class attribute
1.3 Static methods through class classes: forname (String className) (Common)
1. <span style="font-size:18px;">Package Fanshe; 2./** 3. * Get three ways of class object 4. * 1 object--> getclass (); 5. * 2 any data type (including basic data types) has a "static" Class attribute of 6. * 3 static method via class: Forname (String className) (common) 7. * 8. */ 9. Public classFanshe {Ten. Public Static voidMain (string[] args) { One.//The first way to get the class object A. Student STU1 =NewStudent ();//This new creates a student object, a class object. -. Class Stuclass = Stu1.getclass ();//Get Class object -. System. out. println (Stuclass.getname ()); the. -.//The second way to get the class object -. Class stuClass2 = Student.class; -. System. out. println (Stuclass = = StuClass2);//determines whether the first method gets the class object and the second method gets the same +. -.//The Third way to get the class object +.Try { A. Class STUCLASS3 = Class.forName ("Fanshe. Student");//Note that this string must be a real path, which is the classpath with the package name, the package name. Class Name at. System. out. println (STUCLASS3 = = StuClass2);//determine if three ways to get the same class object -. }Catch(ClassNotFoundException e) { -. E.printstacktrace (); -. } -. -. } in. }</span>Note: During a run, only one class object is generated.
Three ways to use the third, the first object has to reflect what to do. The second type of package that needs to be imported is too strong to throw a compile error without a packet. Generally the third type, a string can be passed in also can be written in the configuration file medium multiple methods.
Reprinted article: http://blog.csdn.net/sinat_38259539/article/details/71799078
The reflection mechanism of Java