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

How to use java reflection to get objects

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

Most people do not understand the knowledge points of this article "how to use java reflection to get objects", so the editor summarizes the following contents, detailed content, clear steps, and has a certain reference value. I hope you can get something after reading this article. Let's take a look at this "how to use java reflection to get objects" article.

The operating environment of this tutorial: windows7 system, java10 version, DELL G3 computer.

1. Use the object to call the getClass method of Object

How to get a bytecode object: to use this method, you must specify the specific category and create the object.

Public static void getClassObject_1 () {Person p = new Person (); Class clazz = p.getClass (); Person p1 = new Person (); Class clazz1 = p1.getClass (); System.out.println (clazz = = clazz1);}

2. Call attributes

Any data type has static properties. Class can get the corresponding class object. It's relatively simple, but explicitly use static members in a category.

Public static void getClassObject_2 () {Class clazz = Person.class; Class clazz1 = Person.class;System.out.println (clazz = = clazz1); / / true are all bytecodes of the Person class}

3. Adopt forName () method.

It can be obtained by the string name of a given category and can be further extended. But it is done in the Class class. This method is forName. This method can be more convenient and extensible as long as it has a name.

Public static void getClassObject_3 () throws ClassNotFoundException {String className = "com.ldb.po.Person"; Class clazz = Class.forName (className); System.out.println (clazz);} the above is about "how to use java reflection to get objects". I believe you all have some understanding. I hope the content shared by the editor will be helpful to you. If you want to know more about it, please follow the industry information channel.

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