Field setFloat() method in Java with Examples Last Updated : 13 Jan, 2023 Comments Improve Suggest changes Like Article Like Report setFloat() method of java.lang.reflect.Field used to set the value of a field as a float on the specified object. When you need to set the value of a field of an object as a float then you can use this method to set value over an Object. Syntax: public void setFloat(Object obj, float f) throws IllegalArgumentException, IllegalAccessException Parameters: This method accepts two parameters: obj: which is the object whose field should be modified andf: which is the new value for the field of obj being modified. Return: This method returns nothing. Exception: This method throws the following Exception: IllegalAccessException: if this Field object is enforcing Java language access control and the underlying field is either inaccessible or final.IllegalArgumentException: if the specified object is not an instance of the class or interface declaring the underlying field (or a subclass or implementer thereof), or if an unwrapping conversion fails.NullPointerException: if the specified object is null and the field is an instance field.ExceptionInInitializerError: if the initialization provoked by this method fails. Below programs illustrate setFloat() method: Program 1: Java // Java program to illustrate setFloat() method import java.lang.reflect.Field; public class GFG { public static void main(String[] args) throws Exception { // create user object Employee emp = new Employee(); // print value of salary System.out.println( "Value of salary before " + "applying setFloat is " + emp.salary); // Get the marks field object Field field = Employee.class.getField("salary"); // Apply setFloat Method field.setFloat(emp, 259939.99f); // print value of salary System.out.println( "Value of salary after " + "applying setFloat is " + emp.salary); // print value of pf System.out.println( "Value of pf before " + "applying setFloat is " + emp.pf); // Get the marks field object field = Employee.class.getField("pf"); // Apply setFloat Method field.setFloat(emp, 234234.34f); // print value of pf System.out.println( "Value of pf after " + "applying setFloat is " + emp.pf); } } // sample class class Employee { // static float values public static float pf = 122342.89f; public static float salary = 143125f; } Output:Value of salary before applying setFloat is 143125.0 Value of salary after applying setFloat is 259939.98 Value of pf before applying setFloat is 122342.89 Value of pf after applying setFloat is 234234.34 Program 2: Java // Java program to illustrate setFloat() method import java.lang.reflect.Field; public class GFG { public static void main(String[] args) throws Exception { // create Numbers object Numbers no = new Numbers(); // Get the value field object Field field = Numbers.class .getField("value"); // Apply setFloat Method field.setFloat(no, 3244.466f); // print value of isActive System.out.println( "Value after " + "applying setFloat is " + Numbers.value); } } // sample Numbers class class Numbers { // static float value public static float value = 26774.3685f; } Output:Value after applying setFloat is 3244.466 Reference: https://docs.oracle.com/javase/8/docs/api/java/lang/reflect/Field.html#setFloat-java.lang.Object-float- Comment More infoAdvertise with us Next Article Field setFloat() method in Java with Examples A AmanSingh2210 Follow Improve Article Tags : Java Java-Functions java-lang-reflect-package Java-Field Practice Tags : Java Similar Reads Field set() method in Java with Examples The set() method of java.lang.reflect.Field is used to set the value of the field represented by this Field object on the specified object argument to the specified new value passed as parameter. The new value is automatically unwrapped if the underlying field has a primitive type. If the field is s 4 min read Field setLong() method in Java with Examples setLong() method of java.lang.reflect.Field used to set the value of a field as a long on the specified object. When you need to set the value of a field of an object as long then you can use this method to set value over an Object. Syntax: public void setLong(Object obj, long l) throws IllegalArgum 3 min read Field setInt() method in Java with Examples setInt() method of java.lang.reflect.Field used to set the value of a field as an int on the specified object. When you need to set the value of a field of an object as int then you can use this method to set value over an Object. Syntax: public void setInt(Object obj, int i) throws IllegalArgumentE 3 min read Field setByte() method in Java with Examples setByte() method of java.lang.reflect.Field used to set the value of a field as a byte on the specified object. When you need to set the value of a field of an object as byte then you can use this method to set value over an Object. Syntax: public void setByte(Object obj, byte b) throws IllegalArgum 3 min read Field setDouble() method in Java with Examples setDouble() method of java.lang.reflect.Field used to set the value of a field as a double on the specified object. When you need to set the value of a field of an object as double then you can use this method to set value over an Object. Syntax: public void setDouble(Object obj, double d) throws Il 3 min read Like