Java tutorial
//package com.java2s; import java.lang.reflect.Field; public class Main { @SuppressWarnings("unchecked") public static <E> E getProperty(Object object, String fieldName) { Class<?> clazz = object.getClass(); while (clazz != null) { try { Field field = clazz.getDeclaredField(fieldName); field.setAccessible(true); return (E) field.get(object); } catch (NoSuchFieldException e) { clazz = clazz.getSuperclass(); } catch (Exception e) { throw new IllegalStateException(e); } } return null; } }