This article is excerpted from Java reflection tutorial
Through the reflection mechanism of Java, you can obtain information such as class, interface, fields, and methods at runtime even if you do not know information such as class name and method name during compilation, you can also create new instances, call methods, and obtain/set attribute values.
This article describes the basic knowledge of the Java reflection mechanism, including: array (arrays), annotation (annotations), generic (generics), and dynamic proxy (Dynamic proxy ), and dynamic class loading and reloading ).
This article also introduces some special techniques, including getting the getter method of a class and accessing the private attributes and methods of a class.
This article introduces Java reflection based on JDK 6.
Example
First, let's look at an example of Java reflection.
Import Java. lang. reflect. method; public class hellojava {public static void main (string [] ARGs) {method [] Methods = hellojava. class. getmethods (); For (method M: Methods) system. out. println (M. getname ());}}
Result:
Mainwaitwaitwaitpolicstostringhashcodegetclassnotifypolicyall
This article will be divided into the following parts to introduce the reflection mechanism of Java
- In-depth exploration of Java reflection mechanism (class)
- Non-in-depth exploration of Java reflection mechanisms (constructors)