Open In App

Field setDouble() method in Java with Examples

Last Updated : 10 Jan, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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:

  • 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 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 setDouble() method: 

Program 1: 

Java




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




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



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads