- Get and set Fields using reflection in java
- Get all fields of the class
- Get particular field of the class
- Getting and setting Field value
- Getting and setting static Field value
- Was this post helpful?
- Share this
- Related Posts
- Author
- Related Posts
- Access private fields and methods using reflection in java
- Invoke constructor using Reflection in java
- Invoke Getters And Setters Using Reflection in java
- How to invoke method using reflection in java
- Java Reflection tutorial
- Java Reflection — Fields
- Obtaining Field Objects
- Field Name
- Field Type
- Getting and Setting Field Values
Get and set Fields using reflection in java
In this post, we will see how to get and set fields using reflection in java.
java.lang.reflect.Field can be used to get/set fields(member variables) at runtime using reflection.
Get all fields of the class
All fields of the class can be obtained from the Class object. Here is an example.
Field[] will have all the public fields of the class.
Get particular field of the class
If you already know name of the fields you want to access, you can use cl.getField(String fieldName) to get Field object.
Getting and setting Field value
Field.get() and Field.set() methods can be used get and set value of field respectively.
Let’s understand with the help of example.
Consider a class named Employee which consists of two private fields name and age .
Create main class named PrivateFieldReflectionMain
> catch ( SecurityException | IllegalArgumentException | IllegalAccessException | NoSuchFieldException e )
When you run above class, you will get below output:
Output:
Please note that Here parameter to get() and set() should be object of the class to which field belongs.
For example:
In this case, name field belongs to Employee class, we have passed e to get() and set() method of field object.
Getting and setting static Field value
In case, you want to get and set static field, then you need to pass null while using get() and set() method of Field.
For example:
then you can get and set field as below:
That’s all about how to get and set Fields using reflection in java.
Was this post helpful?
Share this
Related Posts
Author
Related Posts
Access private fields and methods using reflection in java
Table of ContentsAccess private fieldAccess private method In this post, we will see how to access private fields and methods using reflection in java. Can you access private fields and methods using reflection? Yes, you can. It is very easy as well. You just need to call .setAccessible(true) on field or method object which you […]
Invoke constructor using Reflection in java
Table of ContentsInstantiate Constructor with no parametersInstantiate Constructor with parametersConstructor parameters for primitive typesConclusion In this post, we will see how to invoke constructor using reflection in java. You can retrieve the constructors of the classes and instantiate object at run time using reflection. java.lang.reflect.Constructor is used to instantiate the objects. Instantiate Constructor with no […]
Invoke Getters And Setters Using Reflection in java
Table of ContentsUsing PropertyDescriptorUsing Class’s getDeclaredMethods In this post, we will see how to call getters and setters using reflection in java. We have already seen how to invoke method using reflection in java. There are two ways to invoke getter and setter using reflection in java. Using PropertyDescriptor You can use PropertyDescriptor to call […]
How to invoke method using reflection in java
Table of ContentsInvoke method without parametersInvoke method with parametersInvoke static method using reflectionInvoke private method using reflection In this post, we will see how to invoke the method using reflection in java. Let’s understand this with the help of the example. Create a class named Employee.java. We will invoke this class’s method using reflection. [crayon-64bb6e4d33844159583717/] […]
Java Reflection tutorial
Table of ContentsIntroductionThe ‘reflect’ packageFieldModifierProxyMethodConstructorArrayUses of Reflection in JavaExtensibilityDeveloping IDEs and Class BrowsersDebugging and Testing toolsDisadvantages of ReflectionLow PerformaceSecurity RiskExposure of Internal Fields and AttributesJava.lang.Class – the gateway to ReflectionImportant Methods of java.lang.ClassforName() method:getConstructors() and getDeclaredConstructors() methods:getMethods() and getDeclaredMethods()getFields() and getDeclaredFields() methods:Conclusion Introduction Reflection, as we know from the common term, is an image of […]
Java Reflection — Fields
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 Java Field 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 object Field[] fields = 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.class Field field = aClass.getField("someField");
The example above will return the Field instance corresponding to the field someField as declared in the MyObject below:
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 object String 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() and Field.set() methods, like this:
Class aClass = MyObject.class Field 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 the objectInstance parameter passed above.