Java tutorial
//package com.java2s; //License from project: Apache License import java.lang.reflect.Field; public class Main { public static void setMember(Object object, String memberName, Object value) throws IllegalAccessException, NoSuchFieldException { Field declaredField = getField(object.getClass(), memberName); declaredField.setAccessible(true); declaredField.set(object, value); } public static Field getField(Class<?> klass, String member) throws NoSuchFieldException { try { return klass.getDeclaredField(member); } catch (NoSuchFieldException e) { if (klass.getSuperclass() != null) return getField(klass.getSuperclass(), member); else throw new NoSuchFieldException(String.format("Class does not contain member %s!", member)); } } }