Field: getDouble(Object obj) : Field « java.lang.reflect « Java by API






Field: getDouble(Object obj)

 

import java.lang.reflect.Field;

public class Main {
  public static void main(String[] args) throws Exception {
    Object clazz = new TestClass();
    String lookingForValue = "firstValue";

    Field field = clazz.getClass().getField(lookingForValue);
    Class clazzType = field.getType();
    if (clazzType.toString().equals("double"))
      System.out.println(field.getDouble(clazz));
    else if (clazzType.toString().equals("int"))
      System.out.println(field.getInt(clazz));
    
    //System.out.println(field.get(clazz));
  }
}

class TestClass {
  public double firstValue = 3.14;
}

   
  








Related examples in the same category

1.Field: get(Object obj)
2.Field: getAnnotation(Class annotationClass)
3.Field: getGenericType()
4.Field: getInt(Object obj)
5.Field: getModifiers()
6.Field: Class getType()
7.Field: isEnumConstant()
8.Field: isSynthetic()
9.Field: setAccessible(boolean flag)
10.Field: setBoolean(Object obj, boolean z)
11.Field: setInt(Object obj, int i)