Home > Java > javaTutorial > body text

What is the principle and function of Java reflection?

PHPz
Release: 2023-04-26 23:31:06
forward
817 people have browsed it

1. Process of calling reflection

(1)Preparation phase: Load all classes during compilation and save the meta information of each class to Class In the class object, each class corresponds to a Class object

(2)Get the Class object: Call x.class/x.getClass()/Class.forName() to get the Class of x Object clz (the bottom layers of these methods are all native methods, which are written at the bottom of the JVM. We will not explore the bottom layer of the JVM for now)

(3)Perform actual reflection operations: Obtain the Field/Method/Constructor object through the clz object for further operations

2. Obtain the class class

To obtain the information of a class through reflection, you must first Get the Class class instance corresponding to the class. The Class class instance represents the class and interface of the running Java application. The Class class has no public constructor. The Class class object is in a binary byte stream (usually a .class file, which can also be obtained through the network or zip package) When loaded by the JVM, it is called defineClass of the class loader () method to construct.

/**
 * Instances of the class {@code Class} represent classes and
 * interfaces in a running Java application.
 * {@code Class} has no public constructor. Instead {@code Class}
 * objects are constructed automatically by the Java Virtual Machine as classes
 * are loaded and by calls to the {@code defineClass} method in the class
 * loader.
*/
 
public final class Class<T> implements java.io.Serializable,GenericDeclaration,Type,AnnotatedElement {
    ......
}
Copy after login

The above is the detailed content of What is the principle and function of Java reflection?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:yisu.com
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