Field setBoolean() method in Java with Examples
Last Updated :
10 Jan, 2023
The setBoolean() method of java.lang.reflect.Field used to set the value of a field as a boolean on the specified object. When you need to set the value of a field of an object as boolean then you can use this method to set value over an Object. Syntax:
public void setBoolean(Object obj, boolean z)
throws IllegalArgumentException,
IllegalAccessException
Parameters: This method accepts two parameters:
- obj: which is the object whose field should be modified and
- z: which is the new value for the field of obj being modified.
Return value: This method returns nothing. Exception: This method throws following Exception:
- IllegalAccessException: if this Field object is enforcing Java language access control and the underlying field is either inaccessible or final.
- IllegalArgumentException: if the specified object is not an instance of the class or interface declaring the underlying field (or a subclass or implementer thereof), or if an unwrapping conversion fails.
- NullPointerException: if the specified object is null and the field is an instance field.
- ExceptionInInitializerError: if the initialization provoked by this method fails.
Below programs illustrate setBoolean() method: Program 1:
Java
import java.lang.reflect.Field;
public class GFG {
public static void main(String[] args)
throws Exception
{
User user = new User();
System.out.println("Value before "
+ "applying setBoolean is "
+ user.isActive);
Field field
= User. class
.getField("isActive");
field.setBoolean(field, false );
System.out.println("Value after "
+ "applying setBoolean is "
+ user.isActive);
}
}
class User {
public static boolean isActive = true ;
}
|
Output:
Value before applying setBoolean is true
Value after applying setBoolean is false
Program 2:
Java
import java.lang.reflect.Field;
public class GFG {
public static void main(String[] args)
throws Exception
{
Employee emp = new Employee();
System.out.println("Value of isManager before "
+ "applying setBoolean is "
+ emp.isManager);
Field field
= Employee. class
.getField("isManager");
field.setBoolean(emp, false );
System.out.println("Value of isPresent before "
+ "applying setBoolean is "
+ emp.isManager);
System.out.println("Value of isManager before "
+ "applying setBoolean is "
+ emp.isPresent);
field = Employee. class
.getField("isPresent");
field.setBoolean(emp, true );
System.out.println("Value of isPresent before "
+ "applying setBoolean is "
+ emp.isPresent);
}
}
class Employee {
public static boolean isPresent = false ;
public static boolean isManager = true ;
}
|
Output:
Value of isManager before applying setBoolean is true
Value of isPresent before applying setBoolean is false
Value of isManager before applying setBoolean is false
Value of isPresent before applying setBoolean is true
References: https://docs.oracle.com/javase/8/docs/api/java/lang/reflect/Field.html#setBoolean-java.lang.Object-boolean-
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...