Field setShort() method in Java with Examples

The setShort() method of java.lang.reflect.Field is used to set the value of a field as a short on the specified object. When you need to set the value of a field of an object as short then you can use this method to set value over an Object.

Syntax:

public void setShort(Object obj, short s)
         throws IllegalArgumentException,
                IllegalAccessException

Parameters: This method accepts accepts two parameters:

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

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to illustrate setShort() 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 uniqueNo
        System.out.println(
            "Value of uniqueNo before "
            + "applying setShort is "
            + emp.uniqueNo);
  
        // Get the field object
        Field field
            = Employee.class
                  .getField("uniqueNo");
  
        // Apply setShort Method
        field.setShort(emp, (short)134);
  
        // print value of uniqueNo
        System.out.println(
            "Value of uniqueNo after "
            + "applying setShort is "
            + emp.uniqueNo);
    }
}
  
// sample class
class Employee {
  
    // static short values
    public static short uniqueNo = 239;
}

chevron_right


Output:

Value of uniqueNo before applying setShort is 239
Value of uniqueNo after applying setShort is 134

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output:

Value after applying setShort is 5366

References: https://docs.oracle.com/javase/8/docs/api/java/lang/reflect/Field.html#setShort-java.lang.Object-short-

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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.