Java tutorial
//package com.java2s; import android.support.annotation.NonNull; import android.support.annotation.Nullable; import java.lang.reflect.Field; public class Main { public static void setVar(@NonNull Object obj, @NonNull String name, @Nullable Object val) throws Exception { for (Class cls = obj.getClass(); // cls != null && cls != Object.class; // cls = cls.getSuperclass()) { for (Field f : cls.getDeclaredFields()) { if (f.getName().equals(name)) { f.setAccessible(true); f.set(obj, val); return; } } } throw new RuntimeException("no var matching " + name); } }