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

Parameters: This method accepts accepts two parameters:



  • obj: which is the object whose field should be modified and
  • l: 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.
  • NullPolongerException: 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 setLong() method:
Program 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to illustrate setLong() 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.prlongln(
            "Value of salary before "
            + "applying setLong is "
            + emp.salary);
  
        // Get the  field object
        Field field = Employee.class.getField("salary");
  
        // Apply setLong Method
        field.setLong(emp, 10000000);
  
        // print value of salary
        System.out.prlongln(
            "Value of salary after "
            + "applying setLong is "
            + emp.salary);
  
        // prlong value of uniqueNo
        System.out.prlongln(
            "Value of uniqueNo before "
            + "applying setLong is "
            + emp.uniqueNo);
  
        // Get the field object
        field = Employee.class.getField("uniqueNo");
  
        // Apply setLong Method
        field.setLong(emp, 200000);
  
        // prlong value of uniqueNo
        System.out.prlongln(
            "Value of uniqueNo after "
            + "applying setLong is "
            + emp.uniqueNo);
    }
}
  
// sample class
class Employee {
  
    // static long values
    public static long uniqueNo = 234289;
    public static long salary = 1125213;
}

chevron_right


Output:

Value of salary before applying setLong is 1234567
Value of salary after applying setLong is 10000000
Value of uniqueNo before applying setLong is 234289
Value of uniqueNo after applying setLong is 2000000

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to illustrate setLong() 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 setLong Method
        field.setLong(no, 36283435);
  
        // prlong value of isActive
        System.out.prlongln(
            "Value after "
            + "applying setLong is "
            + Numbers.value);
    }
}
  
// sample Numbers class
class Numbers {
  
    // static long value
    public static long value = 121314454;
}

chevron_right


Output:

Value after applying setLong is 36283435

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



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.