Java Reflection (iv): variables

Source: Internet
Author: User

Reprinted from Concurrent Programming network –ifeve.com

Content index
Get Field Object
Variable name
Variable type
Gets or sets the (get/set) variable value

Using the Java reflection mechanism you can check the variable information (member variable) of a class at run time or get or set the value of the variable. The above functionality can be achieved by using the Java.lang.reflect.Field class. In this section you will get an in-depth look at the Field object's information.

Get Field Object

The Field object can be obtained from the class object, as in the following example:

  Class AClass = ...//Get class object  field[] methods = Aclass.getfields ();

The returned Field object array contains a collection of all variables declared as public in the specified class.
If you know the name of the variable you want to access, you can get the specified variable in the following way:

  Class  aclass = myobject.class  Field field = Aclass.getfield ("Somefield");

The above example returns an instance of the field class that corresponds to a member variable named Somefield declared in the MyObject class, that is:

  public class myobject{Public    String somefield = null;  }

When calling the GetField () method, a nosuchfieldexception is thrown if the corresponding variable is not found according to the given method parameter.

Variable name

Once you get the field instance, you can get his variable name by calling the Field.getname () method, as in the following example:

  Field field = ...//Get Field object  String fieldName = Field.getname ();

Variable type

You can get the type of a variable (such as String, int, and so on) by calling the Field.gettype () method.

  Field field = Aclass.getfield ("Somefield");  Object FieldType = Field.gettype ();

Gets or sets the (get/set) variable value

Once you have a reference to a field, you can get or set the value of the variable by calling the Field.get () or Field.set () method, as in the following example:

  Class  aclass = myobject.class  Field field = Aclass.getfield ("Somefield");  MyObject objectinstance = new MyObject ();  Object value = Field.get (objectinstance);  Field.set (objetinstance, value);

The parameter objetinstance of the incoming Field.get ()/field.set () method should be an instance of the class that owns the specified variable. The parameter passed in the above example is an instance of the MyObject class, because Somefield is an instance of the MyObject class.
If the variable is a static variable (public static) then passing NULL as a parameter when calling the Field.get ()/field.set () method does not pass an instance of the class that owns the variable. (Translator Note: You can get the same result if you pass in an instance of the class that owns the variable)

Original address Author: Jakob Jenkov Translator: Yevenhai ([email protected])

Java Reflection (iv): variables

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.