Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the java reflection mechanism

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article introduces what is the java reflection mechanism, the content is very detailed, interested friends can use for reference, I hope it can be helpful to you.

Java: "everything is the object". It feels like the java language itself is constantly practicing this sentence.

The basis of the java reflection mechanism comes from a Class class, and we can use the methods in this class to read, instantiate, and so on.

Object represents or encapsulates some data. After a class is loaded, jvm creates a Class object for the corresponding class (a class corresponds to only one Class object), and the entire structure information of the class is placed in the corresponding Class object. The Class object is like a mirror through which you can see all the information of the corresponding class.

Free video tutorials sharing: java video tutorials

For example:

The process of instantiating a Person:

Person person = new Person ()

Apply reflection to achieve:

Class clz = null;String name = "com.zl.server.Person"; / / the address of the class try {clz = Class.forName (name); / / pass the address of the class to Person person = (Person) clz.getConstructor (). NewInstance (); / / get the constructor and instantiate return servlet;} catch (Exception e) {e.printStackTrace ();}

The reflection mechanism makes class instantiation more flexible.

In many frameworks, reflection has been used a lot, and we can get the annotations, constructors, attributes and so on of the class through Class for more processing.

On what is the java reflection mechanism to share here, I hope that the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report