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 IllegalArgumentException,
                   IllegalAccessException

Parameters: This method accepts accepts two parameters:



  • obj: which is the object whose field should be modified and
  • d: 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 implementor 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 setDouble() method:
Program 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to illustrate setDouble() 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 setDouble is "
            + emp.salary);
  
        // Get the field object
        Field field = Employee.class
                          .getField("salary");
  
        // Apply setDouble Method
        field.setDouble(emp, 50000.99);
  
        // print value of salary
        System.out.println(
            "Value of salary after "
            + "applying setDouble is "
            + emp.salary);
  
        // print value of pf
        System.out.println(
            "Value of pf before "
            + "applying setDouble is "
            + emp.pf);
  
        // Get the field object
        field = Employee.class.getField("pf");
  
        // Apply setDouble Method
        field.setDouble(emp, 1234.34);
  
        // print value of pf
        System.out.println(
            "Value of pf after "
            + "applying setDouble is "
            + emp.pf);
    }
}
  
// sample class
class Employee {
  
    // static double values
    public static double pf = 2342.89;
    public static double salary = 43125;
}

chevron_right


Output:

Value of salary before applying setDouble is 43125.0
Value of salary after applying setDouble is 50000.99
Value of pf before applying setDouble is 2342.89
Value of pf after applying setDouble is 1234.34

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to illustrate setDouble() 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 setDouble Method
        field.setDouble(no, 53245.466);
  
        // print value of isActive
        System.out.println(
            "Value after "
            + "applying setDouble is "
            + Numbers.value);
    }
}
  
// sample Numbers class
class Numbers {
  
    // static double value
    public static double value = 1232.3685;
}

chevron_right


Output:

Value after applying setDouble is 53245.466

Reference: https://docs.oracle.com/javase/8/docs/api/java/lang/reflect/Field.html#setDouble-java.lang.Object-double-



My Personal Notes arrow_drop_up

I am a Developer I love to code and bring my ideas alive

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.