Java Reflection - Java Field.setFloat(Object obj, float f)








Syntax

Field.setFloat(Object obj, float f) has the following syntax.

public void setFloat(Object obj,  float f)  throws IllegalArgumentException ,   IllegalAccessException

Example

In the following code shows how to use Field.setFloat(Object obj, float f) method.

import java.lang.reflect.Field;
//from  ww  w. j  a  v a 2 s .  co  m
class MyClass {
  public float i = 1.123F;
}

public class Main {
  public static void main(String[] args) throws Exception {
    Class<?> clazz = Class.forName("MyClass");
    MyClass x = (MyClass) clazz.newInstance();

    Field f = clazz.getField("i");
    System.out.println(f.getFloat(x));
    
    f.setFloat(x, 9.99F);
    System.out.println(f.getFloat(x)); 

  }
}

The code above generates the following result.