#What reflection is java?
Reflection is one of the features of Java and is a mechanism for indirectly operating target objects.
The JAVA reflection mechanism is that in the running state, for any entity class, all properties and methods of this class can be known; for any object, any method and property of it can be called; this kind of dynamic The function of obtaining information and dynamically calling object methods is called the reflection mechanism of the Java language.
Why is reflection needed in Java? What problem does reflection solve?
In one sentence, reflection can give jvm the ability to compile dynamically. Otherwise, the metadata information of the class can only be achieved by static compilation, such as hot loading, Tomcat's Classloader, etc. cannot support
There are two types of compilation in Java:
● Static compilation: The type is determined at compile time, and the bound object is passed.
● Dynamic compilation: determine the type and bind the object at runtime. Dynamic compilation maximizes the flexibility of Java, embodies polymorphic applications, and can reduce coupling between classes.
Java Reflection is a key property of Java being considered a dynamic (or quasi-dynamic) language. This mechanism allows the program to obtain the internal information of any class with a known name through the Reflection APIs at runtime, including its modifiers (such as public, static, etc.), superclass (such as Object), implemented interfaces (such as Cloneable), and All information about fields and methods, and can change the contents of fields or invoke methods at runtime.
Reflection can load, detect, and use classes that are completely unknown during compilation at runtime. That is, a Java program can load a class whose name is known only at runtime, obtain its complete structure, and generate its object entity, or set values for its fields, or invoke its methods.
Reflection allows static languages to inspect and modify the structure and behavior of the program at runtime.
In static languages, when using a variable, you must know its type. In Java, the type information of variables is saved in the class file during compilation, so that it can be accurate at runtime; in other words, the behavior of the program at runtime is fixed. If you want to change it at runtime, you need reflection.
The classes that implement the Java reflection mechanism are all located in the java.lang.reflect package:
1. Class class: represents a class
2. Field class: represents a class Member variables (properties of the class)
3. Method class: represents the method of the class
4. Constructor class: represents the construction method of the class
5. Array class: provides To dynamically create arrays and access static methods of array elements
Use
1. Three ways to obtain Class objects
1.1 Object ——> getClass();
1.2 Any data type (including basic data types) There is a "static" class attribute
1.3 Through the static method of the Class class: forName (String className) (commonly used)
/** * 获取Class对象的三种方式 * 1 Object ——> getClass(); * 2 任何数据类型(包括基本数据类型)都有一个“静态”的class属性 * 3 通过Class类的静态方法:forName(String className)(常用) * */ public class Fanshe { public static void main(String[] args) { //第一种方式获取Class对象 Student stu1 = new Student();//这一new 产生一个Student对象,一个Class对象。 Class stuClass = stu1.getClass();//获取Class对象 System.out.println(stuClass.getName()); //第二种方式获取Class对象 Class stuClass2 = Student.class; System.out.println(stuClass == stuClass2); //判断第一种方式获取的Class对象和第二种方式获取的是否是同一个 //第三种方式获取Class对象 try { Class stuClass3 = Class.forName("fanshe.Student"); //注意此字符串必须是真实路径,就是带包名的类路径,包名.类名 System.out.println(stuClass3 == stuClass2); //判断三种方式是否获取的是同一个Class对象 } catch (ClassNotFoundException e) { e.printStackTrace(); } System.out.println("*****************获取公有、无参的构造方法*************"); Constructor con = clazz.getConstructor(null); //1>、因为是无参的构造方法所以类型是一个null,不写也可以:这里需要的是一个参数的类型,切记是类型 //2>、返回的是描述这个无参构造函数的类对象。 System.out.println("*************获取公有字段**并调用*****************"); Field f = stuClass.getField("name"); System.out.println(f); System.out.println("***************获取私有的show4()方法******************"); m = stuClass.getDeclaredMethod("show4", int.class); System.out.println(m); m.setAccessible(true);//解除私有限定 Object result = m.invoke(obj, 20);//需要两个参数,一个是要调用的对象(获取有反射),一个是实参 System.out.println("返回值:" + result); } }
The above is the detailed content of What reflection is java?. For more information, please follow other related articles on the PHP Chinese website!