Close

Java Reflection - Field.getDouble() Examples

Java Reflection Java Java API 


Class:

java.lang.reflect.Field

java.lang.Objectjava.lang.Objectjava.lang.reflect.AccessibleObjectjava.lang.reflect.AccessibleObjectjava.lang.reflect.AnnotatedElementAnnotatedElementjava.lang.reflect.Fieldjava.lang.reflect.Fieldjava.lang.reflect.MemberMemberLogicBig

Method:

public double getDouble(Object obj)
                 throws IllegalArgumentException,
                        IllegalAccessException

Gets the value of a static or instance field of type double or of another primitive type convertible to type double via a widening conversion.


Examples


package com.logicbig.example.field;

import java.lang.reflect.Field;

public class GetDoubleExample {
private double aDouble = 2;
private static final double PI = Math.PI;

public static void main(String... args) throws NoSuchFieldException, IllegalAccessException {
Field field = GetDoubleExample.class.getDeclaredField("aDouble");
double d = field.getDouble(new GetDoubleExample());
System.out.println(d);

//static field
Field field2 = GetDoubleExample.class.getDeclaredField("PI");
double d2 = field2.getDouble(null);
System.out.println(d2);
}
}

Output

2.0
3.141592653589793




Widening type conversion:

package com.logicbig.example.field;

import java.lang.reflect.Field;

public class GetDoubleExample2 {
private int anInt = 3;

public static void main(String... args) throws NoSuchFieldException, IllegalAccessException {
Field field = GetDoubleExample2.class.getDeclaredField("anInt");
double d = field.getDouble(new GetDoubleExample2());
System.out.println(d);
}
}

Output

3.0




See Also