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.


/* ww w .j  a  v a 2s.  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.





















Home »
  Java Tutorial »
    java.lang.reflect »




Array
Constructor
Field
Method
Modifier
ParameterizedType
TypeVariable