Java Reflection - Fields
来源:互联网 发布:unity3d 5.3.4 下载 编辑:程序博客网 时间:2024/06/05 15:48
http://tutorials.jenkov.com/java-reflection/fields.html
Using Java Reflection you can inspect the fields (member variables) of classes and get / set them at runtime. This is done via the Java class java.lang.reflect.Field
. This text will get into more detail about the JavaField
object. Remember to check the JavaDoc from Sun out too.
Obtaining Field Objects
The Field
class is obtained from the Class
object. Here is an example:
Class aClass = ...//obtain class objectField[] methods = aClass.getFields();
The Field[]
array will have one Field
instance for each public field declared in the class.
If you know the name of the field you want to access, you can access it like this:
Class aClass = MyObject.classField field = aClass.getField("someField");
The example above will return the Field
instance corresponding to the field someField
as declared in theMyObject
below:
public class MyObject{ public String someField = null;}
If no field exists with the name given as parameter to the getField()
method, a NoSuchFieldException
is thrown.
Field Name
Once you have obtained a Field
instance, you can get its field name using the Field.getName()
method, like this:
Field field = ... //obtain field objectString fieldName = field.getName();
Field Type
You can determine the field type (String, int etc.) of a field using the Field.getType()
method:
Field field = aClass.getField("someField");Object fieldType = field.getType();
Getting and Setting Field Values
Once you have obtained a Field
reference you can get and set its values using the Field.get()
andField.set()
methods, like this:
Class aClass = MyObject.classField field = aClass.getField("someField");MyObject objectInstance = new MyObject();Object value = field.get(objectInstance);field.set(objetInstance, value);
The objectInstance
parameter passed to the get
and set
method should be an instance of the class that owns the field. In the above example an instance of MyObject
is used, because the someField
is an instance member of the MyObject
class.
It the field is a static field (public static ...) pass null
as parameter to the get
and set
methods, instead of theobjectInstance
parameter passed above.
- Java Reflection - Fields
- Java Reflection 教程(4):Fields 成员变量
- Java reflection: accessing private fields and methods
- Java Reflection - Private Fields and Methods
- Java Reflection - Private Fields and Methods
- Java Reflection Tutorial for Classes, Methods, Fields, Constructors, Annotations and much more
- Java Reflection
- Java Reflection
- Java Reflection
- Java Reflection
- java Reflection
- Java Reflection
- java reflection
- Java Reflection
- java reflection
- Java reflection
- Java Reflection
- java reflection
- jquery-migrate
- 数据库数据处理故事多
- NOJ2101 7_密码强度(模拟)
- Oracle忘记密码找回
- HDU 5247 找连续数(RMQ+滑窗)
- Java Reflection - Fields
- MYSQL CAST
- Sharepoint2013部署ADFS 报new-sptrustedIdentityTokenIssuer:the trust provider certificate already exist
- Leetcode_027 Remove Element
- 《深入理解mybatis原理》 Mybatis数据源与连接池
- Java Reflection - Methods
- NOJ2106 6_多项式输出(模拟)
- Java Reflection - Getters and Setters
- Java Reflection - Private Fields and Methods