Java Reflection - Java Field.setShort(Object obj, short s)








Syntax

Field.setShort(Object obj, short s) has the following syntax.

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

Example

In the following code shows how to use Field.setShort(Object obj, short s) method.

import java.lang.reflect.Field;
/*www  .j a  v a  2  s . co m*/
class MyClass {
  public short i = 9;
}

public class Main {
  public static void main(String[] args) throws Exception {
    Class<?> clazz = Class.forName("MyClass");
    MyClass x = (MyClass) clazz.newInstance();

    Field f = clazz.getField("i");
    System.out.println(f.getShort(x));
    
    f.setShort(x, (short)9);
    System.out.println(f.getShort(x)); 

  }
}

The code above generates the following result.