The getDouble() method of java.lang.reflect.Field used to get the value of double which has to be static or instance field type. This method also used to get the value of another primitive type convertible to type double via a widening conversion. When a class contains a static or instance double 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 double getDouble(Object obj) throws IllegalArgumentException, IllegalAccessException
Parameters: This method accepts a single parameter obj which is the object to extract the double value from.
Return value: This method returns the value of field converted to type double.
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 double 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 getDouble() method:
Program 1:
// Java program to demonstrate the getDouble() 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 getDouble Method on User Object // to get the value of Marks field double value = field.getDouble(user); // print result System.out.println( "Value of double Field" + " Marks is " + value); // Now Get the Fees field object field = User. class .getField( "Fees" ); // Apply getDouble Method on User Object // to get the value of Fees field value = field.getDouble(user); // print result System.out.println( "Value of double Field" + " Fees is " + value); } } // sample User class class User { // static double values public static double Marks = 34.13 ; public static double Fees = 3413.99 ; public static String name = "Aman" ; public static double getMarks() { return Marks; } public static void setMarks( double marks) { Marks = marks; } public static double getFees() { return Fees; } public static void setFees( double fees) { Fees = fees; } public static String getName() { return name; } public static void setName(String name) { User.name = name; } } |
Value of double Field Marks is 34.13 Value of double Field Fees is 3413.99
Program 2:
// Java program to demonstrate the getDouble() 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 getDouble Method on field Object // to get the value of value field double value = field.getDouble(real); // print result System.out.println( "Value: " + value); } // RealNumbers class static class RealNumbers { // double field public static double value = 9999999.34567 ; // getter and setter methods public static double getValue() { return value; } public static void setValue( double value) { RealNumbers.value = value; } } } |
Value: 9999999.34567
References: https://docs.oracle.com/javase/8/docs/api/java/lang/reflect/Field.html#getDouble-java.lang.Object-