Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • Java Arrays
  • Java Strings
  • Java OOPs
  • Java Collection
  • Java 8 Tutorial
  • Java Multithreading
  • Java Exception Handling
  • Java Programs
  • Java Project
  • Java Collections Interview
  • Java Interview Questions
  • Java MCQs
  • Spring
  • Spring MVC
  • Spring Boot
  • Hibernate
Open In App
Next Article:
Field setBoolean() method in Java with Examples
Next article icon

Field setDouble() method in Java with Examples

Last Updated : 10 Jan, 2023
Comments
Improve
Suggest changes
Like Article
Like
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-



Next Article
Field setBoolean() method in Java with Examples

A

AmanSingh2210
Improve
Article Tags :
  • Java
  • Java-Field
  • Java-Functions
  • java-lang-reflect-package
Practice Tags :
  • Java

Similar Reads

  • Field set() method in Java with Examples
    The set() method of java.lang.reflect.Field is used to set the value of the field represented by this Field object on the specified object argument to the specified new value passed as parameter. The new value is automatically unwrapped if the underlying field has a primitive type. If the field is s
    4 min read
  • Field setByte() method in Java with Examples
    setByte() method of java.lang.reflect.Field used to set the value of a field as a byte on the specified object. When you need to set the value of a field of an object as byte then you can use this method to set value over an Object. Syntax: public void setByte(Object obj, byte b) throws IllegalArgum
    3 min read
  • Field setBoolean() method in Java with Examples
    The setBoolean() method of java.lang.reflect.Field used to set the value of a field as a boolean on the specified object. When you need to set the value of a field of an object as boolean then you can use this method to set value over an Object. Syntax: public void setBoolean(Object obj, boolean z)
    3 min read
  • Field setLong() method in Java with Examples
    setLong() method of java.lang.reflect.Field used to set the value of a field as a long on the specified object. When you need to set the value of a field of an object as long then you can use this method to set value over an Object. Syntax: public void setLong(Object obj, long l) throws IllegalArgum
    3 min read
  • Field setFloat() method in Java with Examples
    setFloat() method of java.lang.reflect.Field used to set the value of a field as a float on the specified object. When you need to set the value of a field of an object as a float then you can use this method to set value over an Object. Syntax: public void setFloat(Object obj, float f) throws Illeg
    3 min read
  • Field setInt() method in Java with Examples
    setInt() method of java.lang.reflect.Field used to set the value of a field as an int on the specified object. When you need to set the value of a field of an object as int then you can use this method to set value over an Object. Syntax: public void setInt(Object obj, int i) throws IllegalArgumentE
    3 min read
  • Field setChar() method in Java with Examples
    setChar() method of java.lang.reflect.Field used to set the value of a field as a char on the specified object. When you need to set the value of a field of an object as char then you can use this method to set value over an Object. Syntax: public void setChar(Object obj, char c) throws IllegalArgum
    3 min read
  • Field toString() method in Java with Examples
    The toString() method of java.lang.reflect.Field is used to get a string describing this Field. The format of the returned string is the access modifiers for the field, if any, followed by the field type, followed by a space, followed by the fully-qualified name of the class declaring the field, fol
    3 min read
  • Field get() method in Java with Examples
    The get() method of java.lang.reflect.Field used to get the value of the field object. If Field has a primitive type then the value of the field is automatically wrapped in an object. If the field is a static field, the argument of obj is ignored; it may be null Otherwise, the underlying field is an
    3 min read
  • Random nextDouble() method in Java with Examples
    The nextDouble() method of Random class returns the next pseudorandom, uniformly distributed double value between 0.0 and 1.0 from this random number generator's sequence. Syntax: public double nextDouble() Parameters: The function does not accepts any parameter. Return Value: This method returns th
    1 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences