Java Reflection Java Java API
Class:
java.lang.reflect.Field
Method:
public void setBoolean(Object obj,
boolean z)
throws IllegalArgumentException,
IllegalAccessException
Sets the value of a field as a
boolean
on the specified object.
Examples
package com.logicbig.example.field;
import java.lang.reflect.Field;
public class SetBooleanExample { private boolean myBool;
public static void main(String... args) throws NoSuchFieldException, IllegalAccessException { Field f = SetBooleanExample.class.getDeclaredField("myBool"); SetBooleanExample instance = new SetBooleanExample(); f.setBoolean(instance, true); boolean v = f.getBoolean(instance); System.out.println(v); } }
Outputtrue
package com.logicbig.example.field;
import java.lang.reflect.Field;
public class SetBooleanExample2 { private static boolean myStaticBool;
public static void main(String... args) throws NoSuchFieldException, IllegalAccessException { Field f = SetBooleanExample2.class.getDeclaredField("myStaticBool"); f.setBoolean(null, true); System.out.println(f.getBoolean(null)); } }
Outputtrue
|
|