VOOZH about

URL: https://www.geeksforgeeks.org/java/field-setshort-method-in-java-with-examples/

⇱ Field setShort() method in Java with Examples - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Field setShort() method in Java with Examples

Last Updated : 18 Jan, 2023

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  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 implementer thereof), or if an unwrapping conversion fails.
  • NullPoshorterException: if the specified object is null and the field is an instance field.
  • ExceptionInitializerError: if the initialization provoked by this method fails.

Below programs illustrate setShort() method: 

Program 1: 

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

Program 2: 

Comment