Simple use of the Java Field reflection mechanism

Source: Internet
Author: User

code highlighting produced by actipro codehighlighter  (freeware) http://www. codehighlighter.com/-->import java.lang.reflect.field;public class fieldtest {     int i;    public float f;     protected boolean b;    private string s;         public fieldtest ()     {         i=0;        f=0.0f;         b = true;        s =  "";     }    public static void main (String[] args) {         //class instantiation:        fieldtest  Fieldtest = new fieldtest ();         //gets the domain of the class         field[ ] declaredfields = fieldtest.class.getdeclaredfields ();         for (int i=0;i<declaredfields.length;i++) {             Field field = declaredFields[i];             system.out.println ("name is:" +field.getname ());             class fieldtype = field.gettype ();             system.out.println ("Type:" +field.gettype ());             boolean isTurn = true;             while (Isturn)              {                 try{                     isTurn = false;                     system.out.println ("The value before modification is:" + field.get (FieldTest)) ;                     if (Fieldtype.equals (int.class))                      {                         system.out.println (" Use the Setint () method to modify the value of the member variable. ");                  &nBsp;      field.setint (fieldtest, 124);                     }                     else if ( Fieldtype.equals (Float.class))                      {                         system.out.println ("Using SetFloat () Method modifies the value of the member variable. ");                          field.setfloat (fieldtest, 9.9f);                     }else if ( Fieldtype.equals(Boolean.class))                      {                 &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;SYSTEM.OUT.PRINTLN ("Use the SetBoolean () method to modify the value of the member variable.");                          field.setboolean (Fieldtest, false);                     }                     else                      {                     &NBSP;&NBSP;&NBSP;&NBSP;SYSTEM.OUT.PRINTLN ("Use the Set () method to modify the value of the member variable.");                          field.set (fieldtest,  "Nwq");                     }                      SYSTEM.OUT.PRINTLN ("Modified value:" +field.get (fieldtest));                 }catch (exception e)                  {                     system.out.println ("An exception occurred while setting the member variable, Executes the setaccessible () method. ");                     //Modify Accessibility, if true, all members of the current domain can access                      field.setaccessible ( True);                     isTurn = true;                 }            }             system.out.println ("**************");         }    }}


Simple use of the Java Field reflection mechanism

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.