Java tutorial
//package com.java2s; //License from project: Open Source License import java.lang.reflect.Field; public class Main { public static void setField(Object obj, String fieldName, Object value) throws NoSuchFieldException, IllegalAccessException { prepareField(obj.getClass(), fieldName).set(obj, value); } private static Field prepareField(Class c, String fieldName) throws NoSuchFieldException { while (c != null) { try { Field f = c.getDeclaredField(fieldName); f.setAccessible(true); return f; } catch (Exception e) { } finally { c = c.getSuperclass(); } } throw new NoSuchFieldException(); } }