Java Reflection - Java Field.getByte(Object obj)


Field.getByte(Object obj) has the following syntax.

public byte getByte(Object obj)  throws IllegalArgumentException ,   IllegalAccessException


In the following code shows how to use Field.getByte(Object obj) method.

// w  w  w .j a  v  a  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.