get Field By Field Name - Java Reflection

Java examples for Reflection:Field Get

Description

get Field By Field Name

Demo Code


//package com.java2s;
import java.lang.reflect.Field;

public class Main {
    public static Field getFieldByFieldName(Object obj, String fieldName) {
        for (Class<?> superClass = obj.getClass(); superClass != Object.class; superClass = superClass
                .getSuperclass()) {/*from   w  w w.  j a  v a  2  s. c o m*/
            try {
                return superClass.getDeclaredField(fieldName);
            } catch (NoSuchFieldException e) {
            }
        }
        return null;
    }
}

Related Tutorials