Close

Java Reflection - Field.getFloat() 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 float getFloat(Object obj)
               throws IllegalArgumentException,
                      IllegalAccessException

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


Examples


package com.logicbig.example.field;

import java.lang.reflect.Field;

public class GetFloatExample {
private float aFloat = 1.3f;
private int anInt = 2;
private static float aStaticFloat = 2.3f;

public static void main(String... args) throws NoSuchFieldException, IllegalAccessException {
Class<GetFloatExample> c = GetFloatExample.class;
GetFloatExample instance = new GetFloatExample();

//instance field
Field field = c.getDeclaredField("aFloat");
float f = field.getFloat(instance);
System.out.println(f);

//type conversion
Field field2 = c.getDeclaredField("anInt");
float f2 = field2.getFloat(instance);
System.out.println(f2);

//static field
Field field3 = c.getDeclaredField("aStaticFloat");
float f3 = field3.getFloat(null);
System.out.println(f3);
}
}

Output

1.3
2.0
2.3




See Also