Field getFloat() method in Java with Examples
The getFloat() method of java.lang.reflect.Field used to get the value of float which has to be static or instance field type. This method also used to get the value of another primitive type convertible to type float via a widening conversion. When a class contains a static or instance float field and we want to get the value of that field then we can use this method to return the value of Field.
Syntax:
public float getFloat(Object obj)
throws IllegalArgumentException,
IllegalAccessException
Parameters: This method accepts a single parameter obj which is the object to extract the float value from.
Return value: This method returns the value of field converted to type float.
Exception: This method throws following Exception:
- IllegalAccessException: This exception is thrown if Field object is enforcing Java language access control and the underlying field is inaccessible.
- IllegalArgumentException: This exception is thrown if the specified object is not an instance of the class or interface declaring the underlying field or if the field value cannot be converted to the type float by a widening conversion.
- NullPointerException: This exception is thrown if the specified object is null and the field is an instance field.
- ExceptionInInitializerError: This exception is thrown if the initialization provoked by this method fails.
Below programs illustrate getFloat() method:
Program 1:
// Java program to demonstrate the getFloat() method import java.lang.reflect.Field; public class GFG { public static void main(String[] args) throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException { // Create the User class object User user = new User(); // Get the marks field object Field field = User.class.getField("Marks"); // Apply getFloat Method on User Object // to get the value of Marks field float value = field.getFloat(user); // print result System.out.println("Value of float Field" + " Marks is " + value); // Now Get the Fees field object field = User.class.getField("Fees"); // Apply getFloat Method on User Object // to get the value of Fees field value = field.getFloat(user); // print result System.out.println("Value of float Field" + " Fees is " + value); } } // sample User class class User { // static float values public static float Marks = 94.13f; public static float Fees = 1343413.99f; public static String name = "Aman"; public static float getMarks() { return Marks; } public static void setMarks(float marks) { Marks = marks; } public static float getFees() { return Fees; } public static void setFees(float fees) { Fees = fees; } public static String getName() { return name; } public static void setName(String name) { User.name = name; } } |
Value of float Field Marks is 94.13 Value of float Field Fees is 1343414.0
Program 2:
// Java program to demonstrate the getFloat() method import java.lang.reflect.Field; public class GFG { public static void main(String[] args) throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException { // Create the RealNumbers class object RealNumbers real = new RealNumbers(); // Get the value field object Field field = RealNumbers.class .getField("value"); // Apply getFloat Method on field Object // to get the value of value field float value = field.getFloat(real); // print result System.out.println("Value: " + value); } // RealNumbers class static class RealNumbers { // float field public static float value = 123432123.1234f; // getter and setter methods public static float getValue() { return value; } public static void setValue(float value) { RealNumbers.value = value; } } } |
Value: 1.2343212E8
References: https://docs.oracle.com/javase/8/docs/api/java/lang/reflect/Field.html#getFloat-java.lang.Object-
Recommended Posts:
- ByteBuffer getFloat() method in Java with Examples
- Field get() method in Java with Examples
- Field set() method in Java with Examples
- Array getFloat() Method in Java
- Field setInt() method in Java with Examples
- Field setLong() method in Java with Examples
- Field getShort() method in Java with Examples
- Field getModifiers() method in Java with Examples
- Field isSynthetic() method in Java with Examples
- Field getType() method in Java with Examples
- Field setDouble() method in Java with Examples
- Field isEnumConstant() method in Java with Examples
- Field setByte() method in Java with Examples
- Field getByte() method in Java with Examples
- Field getName() method in Java with Examples
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


