Home  >  Article  >  Java  >  How to use reflection functions to dynamically create and call objects in Java

How to use reflection functions to dynamically create and call objects in Java

PHPz
PHPzOriginal
2023-10-24 09:28:44848browse

How to use reflection functions to dynamically create and call objects in Java

How to use reflection functions in Java to dynamically create and call objects

Introduction:
In Java programming, reflection is a powerful technology. It allows us to obtain and manipulate class information at runtime. Among them, the dynamic creation and invocation of objects is one of the important application scenarios of reflection. This article will introduce how to use reflection functions to dynamically create and call objects in Java, and provide specific code examples.

1. Overview of reflection:
Java reflection means that the program operates the relevant properties and methods of the target class through the reflection API at runtime. In the absence of reflection, we must clearly know the specific information of the target class at compile time in order to perform corresponding operations. With reflection, we can dynamically obtain and manipulate target class information at runtime, improving the flexibility and scalability of the program.

2. Use reflection to create objects:
In Java, you can create an object using the newInstance() method of the Class class. The specific steps are as follows:

  1. Obtain the Class object of the target class, which can be achieved through the Class.forName() method or directly calling the class attribute of the target class.

Sample code:

Class<?> clazz = Class.forName("com.example.Person");
  1. Call the newInstance() method to create an object.

Sample code:

Object obj = clazz.newInstance();

Through the above code, we can dynamically create an instance of the Person class without knowing the specific information of the Person class in advance, which improves the flexibility of the program. .

3. Use reflection to dynamically call object methods:
Reflection can not only be used to create objects, but also to call object methods. The following is an example of using reflection to call a method:

  1. Get the Class object of the target class.

Sample code:

Class<?> clazz = Class.forName("com.example.Person");
  1. Get the Method object of the target method.

Sample code:

Method method = clazz.getMethod("setName", String.class);
  1. Call the invoke() method of the Method object and pass in the target object and parameters to dynamically call the target method.

Sample code:

Object obj = clazz.newInstance();
method.invoke(obj, "Tom");

Through the above code, we can dynamically call the setName method of the Person class to set the properties of the object. There is also no need to know the Person class in advance. specific information.

4. Use reflection to obtain and modify object attributes:
Reflection can also be used to obtain and modify object attribute values. The following is an example of using reflection to obtain and modify properties:

  1. Get the Class object of the target class.

Sample code:

Class<?> clazz = Class.forName("com.example.Person");
  1. Get the field object of the target attribute.

Sample code:

Field field = clazz.getDeclaredField("age");
  1. Set a field to be accessible so that its value can be obtained and modified.

Sample code:

field.setAccessible(true);
  1. Use the get() and set() methods of the Field object to obtain and modify the value of the attribute respectively.

Sample code:

Object obj = clazz.newInstance();
int age = (int) field.get(obj);
field.set(obj, age + 1);

Through the above code, we can dynamically obtain and modify the age attribute value of the Person class, and there is no need to know the specific information of the Person class in advance.

Summary:
This article introduces how to use reflection functions to dynamically create and call objects in Java. Through the above example code, readers can understand the basic usage of reflection, and flexibly apply reflection technology in actual project development to improve the flexibility and scalability of the program. Of course, reflection should be used in moderation, because too many reflection operations may have a certain impact on the performance of the program. Therefore, in actual development, we need to reasonably choose whether to use reflection technology according to specific scenarios.

Reference:

  • Oracle official documentation: https://docs.oracle.com/en/java/javase/14/docs/api/java.base/java/ lang/reflect/Method.html
  • Java reflection (Zhihu): https://zhuanlan.zhihu.com/p/149535966

The above is the detailed content of How to use reflection functions to dynamically create and call objects in Java. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn