Reflection Mechanism Summary----Field

Source: Internet
Author: User

The reflection mechanism is very useful to get the information we need when we run it, and the following is a description of field

Field

field, as the name implies, is related to fields, yes, this class can help us get information about the class inside and the member variables

If we want to get the member variable information in a class, first we need to get the class object (Get the class method)

Class clazz = Testreflect.class;
then we can use Clazz to get field.

There are two ways of doing this

<span style= "font-family: ' Microsoft Yahei '; Background-color:rgb (255, 255, 255); " The first of the > is to get a </span><span style= "font-family: ' Microsoft Yahei '; Background-color:rgb (255, 255, 255); " > member Variable </span><span style= "font-family: ' Microsoft Yahei '; Background-color:rgb (255, 255, 255); " > Field object with the parameter </span><span style= "font-family: ' Microsoft Yahei '; Background-color:rgb (255, 255, 255); " > member Variable </span><span style= "font-family: ' Microsoft Yahei '; Background-color:rgb (255, 255, 255); " > Name </span>

The second is to get the Field object for all member variables in this class

The first way

Field field = Clazz.getfield ("field1");

When we just need to get the information of a member variable , we can get the value of the member variable in the following way after getting it.

System.out.println (Field.get (Clazz.newinstance ()));
Get needs to pass in the object of this class, because we get the class object from the beginning, then we can get the object of the class by using the Newinstance method.

There are many methods in the field, such as Getint,getdouble,getchar and so on, which correspond to data types, and can only be obtained with get if they are of type string.

Attention:

1. A member variable in a generic class has an initial value, and if the member variable in the class does not specify initialization, the value obtained by using this method is the initial value, but If you are using get to get a string, there is no initial value (get null)

2. In addition, the use of this method is not possible to obtain a private variable, if the obtained variable is a private variable will be an error

If you want to access a private variable, you need to use the following method (Field2 is a private variable in the class)

Field field = Clazz.getdeclaredfield ("Field2"); Field.setaccessible (true);
Two code can not be less, if the second one is missing or will be an error
This is where you get the value of the private variable.


The second way

field[] fields = Clazz.getfields ();

GetFields can only get public variables, cannot get private variables, if you want to get a private variable, you could use the following statement
Field field = Clazz.getdeclaredfields ("Field2");
This makes it possible to get all the variables declared in the class (but still need to use setaccessible to True when accessing the value)
The following is the way to print out all the variable names and types
for (int i = 0;i < fields.length;i++) {fields[i].setaccessible (true); System.out.println (Fields[i].gettype () + "  " + fields[i].getname () + "=  " + fields[i].get (clazz.newinstance ()) );}
The output is as follows int field1 = 0
int field2 = 2
int field3 = 3



Reflection Mechanism Summary----Field

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.