import java.lang.reflect.Field; public class Bug { String foo; public static void main(String[] args) { Bug a= new Bug(); set(a, "foo", "works"); System.out.println(a.foo); System.out.println(get(a, "foo")); } public static void set(Object instance, String fieldName, Object value) { Field field= getField(instance.getClass(),fieldName); try { field.set(instance, value); } catch (IllegalArgumentException e) { } catch (IllegalAccessException e) { } } public static Object get(Object instance, String fieldName) { Field field= getField(instance.getClass(), fieldName); try { return field.get(instance); } catch (IllegalArgumentException e) { } catch (IllegalAccessException e) { } // Unreachable code return null; } public static Field getField(Class theClass,String fieldName) { Field field= null; try { field= theClass.getDeclaredField(fieldName); } catch (SecurityException e) { } catch (NoSuchFieldException e) { } field.setAccessible(true); return field; } }