Home > Java > javaTutorial > What are the three methods of java reflection

What are the three methods of java reflection

小老鼠
Release: 2024-01-02 16:58:20
Original
658 people have browsed it

There are three methods of java reflection: 1. Get the Class object; 2. Get the constructor, fields and methods of the class; 3. Call methods and access fields through reflection. Detailed introduction: 1. Obtain the Class object: You can obtain the Class object through one of three methods: object acquisition, class name acquisition, and fully qualified name acquisition of the class, so as to obtain information about the class; 2. Obtain the constructor method of the class , Fields and methods: Through the Class object, you can obtain the constructor, fields and methods of the class; 3. Call methods and access fields through reflection, etc.

What are the three methods of java reflection

Operating system for this tutorial: Windows 10 system, Dell G3 computer.

In Java, reflection is a powerful mechanism that allows obtaining class information, calling class methods, accessing class fields, etc. at runtime. Reflection provides three main methods to implement these operations:

1. Obtain the Class object: You can use one of the following three methods to obtain the Class object to obtain information about the class. information.

Get through the object: Use the getClass() method of the object.

MyClass obj = new MyClass();
Class<?> myClass = obj.getClass();
Copy after login

Get by class name: Use the class attribute of the class name.

Class<?> myClass = MyClass.class;
Copy after login

Get through the fully qualified name of the class: use the Class.forName() method.

Class<?> myClass = Class.forName("com.example.MyClass");
Copy after login

2. Obtain the constructor, fields and methods of the class: Through the Class object, you can obtain the constructor, fields and methods of the class.

Get the constructor:

Constructor<?>[] constructors = myClass.getConstructors();
Copy after login

Get the field:

Field[] fields = myClass.getDeclaredFields();
Copy after login

Get the method:

Method[] methods = myClass.getMethods();
Copy after login

3, Call methods and access fields through reflection: Using Method objects and Field objects, you can call class methods and access field values.

Calling method:

Method myMethod = myClass.getMethod("methodName", parameterTypes);
myMethod.invoke(objectInstance, args);
Copy after login

Accessing fields:

Field myField = myClass.getDeclaredField("fieldName");
myField.setAccessible(true); // 如果字段是私有的,需要设置为可访问
Object fieldValue = myField.get(objectInstance);
Copy after login

These three methods provide the flexibility to obtain class information, call methods and access fields at runtime. However, be aware that reflection operations may incur performance overhead and require exception handling when used.

The above is the detailed content of What are the three methods of java reflection. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template