current position:Home>Java reflection - field class use

Java reflection - field class use

2022-01-27 00:57:15 Mssyaa

Field As an operation class in the corresponding class or object in the reflection, or called an attribute

Java SE 8 Of Docs Say so :A Field provides information about, and dynamic access to, a single field of a class or an interface. The reflected field may be a class (static) field or an instance field.

The simple understanding is : We can go through Field Class to class or object field Dynamic operation .

  • About Field Some of the ways :

  A simple case :




  • Something to say :

    • setAccessible Method . yes Field Inherited from AccessibleObject class ,AccessibleObject yes Field、Method、Constuctor The parent of a class . Simple understanding means If the type is private Embellished , You can't have direct access to , You need to set the access permission to true. If it is public You don't need to set .
    • set and get When calling, you need to ensure that you can access , Otherwise, if you can't access it, throw IllegalAccessException.
  • Make a general toString(Object obj) Method



Be careful :

There's a Array class , yes java.lang.reflect Package the next array operation class . The meaning of the call in the code is not difficult to understand . No explanation

  • Calling class




copyright notice
author[Mssyaa],Please bring the original link to reprint, thank you.

Random recommended