Open In App

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  two parameters:

Return: This method returns nothing. 



Exception: This method throws the following Exception:

Below programs illustrate setDouble() method: 

Program 1: 




// 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;
}

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: 




// 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;
}

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-


Article Tags :