Home > Java > Javagetting Started > what is java reflection mechanism

what is java reflection mechanism

王林
Release: 2020-01-19 17:02:55
forward
2327 people have browsed it

what is java reflection mechanism

java: "Everything is an object", I feel that the Java language itself is constantly practicing this sentence.

The basis of the Java reflection mechanism comes from a Class class. We can read, instantiate, etc. each class through the methods in this class.

Object represents or encapsulates some data. After a class is loaded, jvm will create a Class object corresponding to the class (a class will only correspond to one Class object), and the entire structural information of the class will be placed in the corresponding Class object. This Class object is like a mirror, through which you can see all the information of the corresponding class.

Free learning video tutorial sharing: java video tutorial

For example:

The instantiation process of a Person:

Person person = new Person();
Copy after login

Apply reflection to achieve:

Class clz = null;
String name = "com.zl.server.Person";   //类的地址
try {
    clz = Class.forName(name);   //将类的地址传入
    Person person = (Person)clz.getConstructor().newInstance();    //获取构造器,进行实例化
    return servlet;
} catch (Exception e) {
    e.printStackTrace();
}
Copy after login

The reflection mechanism will make the instantiation of classes more flexible.

In many frameworks, reflection has been widely used. We can obtain the annotations, constructors, properties, etc. of the class through Class to perform more processing.

Recommended related articles and tutorials: java introductory tutorial

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

Related labels:
source:csdn.net
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