Java Reflection - Java Field.setByte(Object obj, byte b)








Syntax

Field.setByte(Object obj, byte b) has the following syntax.

public void setByte(Object obj,  byte b)  throws IllegalArgumentException ,   IllegalAccessException

Example

In the following code shows how to use Field.setByte(Object obj, byte b) method.

//from w w  w.  ja va  2 s . c o  m
import java.lang.reflect.Field;

class MyClass {
  public byte i = 10;
}

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.getByte(x)); // Output: 10
    f.setByte(x, (byte)20);
    System.out.println(f.getByte(x)); // Output: 20

  }
}

The code above generates the following result.