Java examples for Reflection:Field
find Field from class by name using reflection
//package com.java2s; import java.lang.reflect.Field; public class Main { public static void main(String[] argv) throws Exception { Class clazz = String.class; String name = "java2s.com"; System.out.println(findField(clazz, name)); }/*from w w w .j av a2 s.c om*/ public static Field findField(Class<?> clazz, String name) { for (Class<?> c = clazz; c != null && !Object.class.equals(c); c = c .getSuperclass()) { Field[] fields = c.getDeclaredFields(); for (Field field : fields) { if (name.equals(field.getName())) return field; } } return null; } }