Close

Java Reflection - Field.setBoolean() Examples

Java Reflection Java Java API 


Class:

java.lang.reflect.Field

java.lang.Objectjava.lang.Objectjava.lang.reflect.AccessibleObjectjava.lang.reflect.AccessibleObjectjava.lang.reflect.AnnotatedElementAnnotatedElementjava.lang.reflect.Fieldjava.lang.reflect.Fieldjava.lang.reflect.MemberMemberLogicBig

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);
}
}

Output

true




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));
}
}

Output

true




See Also