Java Reflection Java Java API
java.lang.reflect.Field
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.
float
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); }}
1.32.02.3