What is Java reflection mechanism? How to use Java reflection mechanism
This article brings you what is the Java reflection mechanism? The method of using Java reflection mechanism has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.
Speaking of the reflection mechanism, people who come into contact with it for the first time may be confused. Reflection? What reflection? How to reflect? What is reflection for? Next, I will talk about Java's reflection machine in this article
But before that, there is still a problem that needs to be solved. The dynamics in the title name. Speaking of dynamics, Let me first introduce dynamic language and static language
##static language
Static language is Languages in which the data type of the variable can be determined at compile time. Most statically typed languages require that the data type must be declared before using the variable. For example: C, Java, Delphi, C#, etc.
Dynamic language
Dynamic language is a language that determines the data type at runtime. There is no need for a type declaration before a variable is used. Usually the type of the variable is the type of the value to which it is assigned. For example, PHP/ASP/Ruby/Python/Perl/ABAP/SQL/JavaScript/Unix Shell, etc. andDynamic language means that the program can change its structure while it is running: new functions can be introduced, existing functions can be deleted and other structural changes.
At this time, you may have questions. Since Java is a static language, how can it be dynamic? One is that Java has a mechanism related to dynamics: Reflection mechanism. Through the reflection mechanism, Java can load, detect and use classes that are completely unknown during compilation when the program is running, and can generate related class object instances, so that its methods can be called or a certain attribute value can be changed. So JAVA can also be regarded as a semi-dynamic language
Let’s talk about the reflection mechanism
Reflection mechanism concept The reflection mechanism in Java means that in the running state, any class can know all the properties and methods of this class; And for any object, any of its methods can be called; this function of dynamically obtaining information and dynamically calling object methods is called the reflection mechanism of the Java language.
The principle of reflection lies in the Class object
Take a look at the process of program loading
Reflection API
The reflection API is used to generate information about classes, interfaces or objects in the JVM. -
Class: The core class of reflection, which can obtain the attributes, methods and other information of the class. -
Field class: A class in the Java.lang.reflec package, which represents the member variables of the class and can be used to get and set attribute values in the class. -
Method class: The class in the Java.lang.reflec package represents the method of the class. It can be used to obtain method information or execute methods in the class. -
Constructor class: Class in the Java.lang.reflec package, representing the construction method of the class.
Let’s talk about how to use reflection
1. Steps
- Get the Class object of the class you want to operate
- Call the method in the Class class
- Use Reflection API to operate this information
2. Method to obtain Class object
1 2 3 4 5 6 7 8 9 10 |
|
3. Get the constructor method, Field, main method and call
Student.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
|
Constructors.java(构造方法)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
|
输出
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
|
Fields.java(字段)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
|
输出
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
|
Main.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
|
The above is the detailed content of What is Java reflection mechanism? How to use Java reflection mechanism. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Analysis of memory leak phenomenon of Java programs on different architecture CPUs. This article will discuss a case where a Java program exhibits different memory behaviors on ARM and x86 architecture CPUs...

When using MyBatis-Plus or other ORM frameworks for database operations, it is often necessary to construct query conditions based on the attribute name of the entity class. If you manually every time...

Field mapping processing in system docking often encounters a difficult problem when performing system docking: how to effectively map the interface fields of system A...

Start Spring using IntelliJIDEAUltimate version...

How to convert names to numbers to implement sorting within groups? When sorting users in groups, it is often necessary to convert the user's name into numbers so that it can be different...

Discussing the hierarchical architecture in back-end development. In back-end development, hierarchical architecture is a common design pattern, usually including controller, service and dao three layers...

Troubleshooting and solutions to the company's security software that causes some applications to not function properly. Many companies will deploy security software in order to ensure internal network security. ...

Solutions to convert names to numbers to implement sorting In many application scenarios, users may need to sort in groups, especially in one...
