Java examples for Reflection:Property
get Value By Property Name
//package com.java2s; import java.lang.reflect.Method; public class Main { public static void main(String[] argv) throws Exception { Object obj = "java2s.com"; String propertyName = "java2s.com"; System.out.println(getValueByPropertyName(obj, propertyName)); }//w w w .j a v a2s . c o m public static Object getValueByPropertyName(Object obj, String propertyName) { String getMethodName = "get" + propertyName.substring(0, 1).toUpperCase() + propertyName.substring(1); Class c = obj.getClass(); try { Method m = c.getMethod(getMethodName); Object value = m.invoke(obj); return value; } catch (Exception e) { e.printStackTrace(); return null; } } }