Java Field.setBoolean(Object obj, boolean z)

Syntax

Field.setBoolean(Object obj, boolean z) has the following syntax.

public void setBoolean(Object obj,  boolean z)  throws IllegalArgumentException ,    IllegalAccessException

Example

In the following code shows how to use Field.setBoolean(Object obj, boolean z) method.


//from   ww w. java2s .  c o 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.





















Home »
  Java Tutorial »
    java.lang.reflect »




Array
Constructor
Field
Method
Modifier
ParameterizedType
TypeVariable