Java Reflection - Java Field.getBoolean(Object obj)








Syntax

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

public boolean getBoolean(Object obj)   throws IllegalArgumentException ,    IllegalAccessException

Example

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

// w  w w .ja v a 2s  . co  m
import java.lang.reflect.Field;

class MyClass {
  public boolean i = true;
}

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.getBoolean(x));
    
    f.setBoolean(x, false);
    System.out.println(f.getBoolean(x)); 

  }
}

The code above generates the following result.