Java Foundation Consolidation Series (iii): Field class for member variable reflection

Source: Internet
Author: User

To use reflection, you first need to get the class object for the Class class object. A Field object corresponds to the attribute (member variable) information of a reflection class


There is a method in this field.setaccessible (Boolean b), this method is very important, there is a comment in the code, is used to improve the speed of Java reflection.

First, we need to define a javabean that the bean uses as our reflection class:

<span style= "Font-family:microsoft yahei;font-size:12px;" >package Com.test;public class Point {private int x;public int y;public point (int x,int y) {super (); this.x = X;this.y = y;}} </span>
we then write a concrete reflection method in main:

<span style= "Font-family:microsoft yahei;font-size:12px;" >package Com.test;import Java.lang.reflect.field;public class Reflecttest {public static void main (String []args) Throws Exception{point pt1 = new Point (3, 5);//The resulting fieldy here is a field on the class (member variable), not the value of Y on instance pt1 field Fieldy = Pt1.getclass (). GetField ("Y"); System.out.println (Fieldy); Output result: public int com.test.Point.yint num = fieldy.getint (PT1); Here you get the value of the variable y above the PT1 instance System.out.println (num); Output: 5//because the member variable x is private, the method as above is not able to get the value of X//Because we need to use the violent reflex field FIELDX = Pt1.getclass (). Getdeclaredfield ("X"); The Getdeclaredfield () method here is to get the field on the class, whether public or private fieldx.setaccessible (true),/** sets the accessible flag of this object to the indicated Boolean value. A value of TRUE indicates that the reflected object should cancel the Java language access check when it is used. A value of false indicates that the reflected object should implement a Java language access check. *///ps:setaccessible is a switch that enables and disables access security checks, is not true to be able to access false and cannot access/through Setaccessible (true) System.out.println (Fieldx.get (PT1)) to improve the reflex speed by closing the security check;}} </span> 
Note that the Setaccessible () method is used when reflecting on the class private member variable x above, which has a specific explanation in the code.


We make a small reflection of the plum, that is, by reflection technology to the string in another class member variable characters in the ' B ' the change to ' A '

<span style= "Font-family:microsoft yahei;font-size:12px;" >package Com.test;public class Point {public String s1= ' Ball ';p ublic string s2 = "Basketball";p ublic string s3= "Lipengl Ong ";} </span>

Below is the main function

<span style= "Font-family:microsoft yahei;font-size:12px;" >package Com.test;import Java.lang.reflect.field;public class Reflecttest {public static void main (String []args) Throws Exception{point pt1 = new Point ();//First Get all methods of a Field array field[] fields = Pt1.getclass (). GetFields (); For (field Field:fields) {///first to determine whether the resulting type is stringif (field.gettype () = = String.class) {//assigns the original variable value to oldvaluestring OldValue = (String Field.get (PT1);//Convert the character in OldValue to character B to astring newvalue = Oldvalue.replace (' B ', ' a ');//Assign a new value to Pt1 Field.set (PT1, newvalue);}} System.out.println (PT1.S1); System.out.println (PT1.S2); System.out.println (PT1.S3);}} </span>

The results are as follows:

Aall
Aasketaall
Lipenglong





Java Foundation Consolidation Series (iii): Field class for member variable reflection

Related Article

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.