Get and Set Field Value using Reflection in Java

  • 31 October 2016
  • ADM

 

Get and Set Field Value using Reflection in Java - images/logos/java.jpg

 

Java Reflection provides classes and interfaces for obtaining reflective information about classes and objects. Reflection allows programmatic access to information about the fields, methods and constructors of loaded classes, and the use of reflected fields, methods, and constructors to operate on their underlying counterparts, within security restrictions. Also provides the possibility to instantiate new objects, invoke methods and get/set field values.

Here is an example how to get and set field values.

Example

For testing will create a simple class with 3 fields: first public, second private and third protected.

package com.admfactory.reflect;

public class Username {

    public int id;
    private String name;
    protected String email;

    public Username() {
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public void display() {
	System.out.println(String.format("Username: [id:%d, name:%s, email:%s]", id, name, email));
    }

}

Here is an example how to set and get the fields using reflection.

package com.admfactory.reflect;

import java.lang.reflect.Field;

public class FieldTest {
    public static void main(String[] args) throws Exception {

	System.out.println("get and set fild values using reflection example");
	Username user = new Username();
	System.out.println();

	Class<Username> clazz = Username.class;
	
	Field fieldID = clazz.getField("id");
	fieldID.set(user, 100);
	
	System.out.println("Id value set using reflection: " + user.id);
	int id = fieldID.getInt(user);
	System.out.println("Id value get using reflection: " + id);
	System.out.println();
	
	Field fieldNAME = clazz.getDeclaredField("name");
	fieldNAME.setAccessible(true);
	fieldNAME.set(user, "Admin");
	
	System.out.println("Name value set using reflection: " + user.getName());
	String name = (String) fieldNAME.get(user);
	System.out.println("Name value get using reflection: " + name);
	
	Field fieldEMAIL = clazz.getDeclaredField("email");
	fieldEMAIL.setAccessible(true);
	fieldEMAIL.set(user, "admin@example.com");
	
	System.out.println("Email value set using reflection: " + user.getEmail());
	String email = (String) fieldEMAIL.get(user);
	System.out.println("Email value get using reflection: " + email);
	
	System.out.println();
	user.display();
    }
}

If you want to get the public field you can use getField method, for private or protected fields you need to use getDeclaredField method. As a best practice you can always use getDeclaredField method. Also for private and protected fields you need to set the field as accessible, otherwise an java.lang.IllegalAccessException exception will be thrown.

Output

get and set fild values using reflection example

Id value set using reflection: 100
Id value get using reflection: 100

Name value set using reflection: Admin
Name value get using reflection: Admin
Email value set using reflection: admin@example.com
Email value get using reflection: admin@example.com

Username: [id:100, name:Admin, email:admin@example.com]

 

References