Field setBoolean() method in Java with Examples
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 implementor 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 program to illustrate setBoolean() method import java.lang.reflect.Field; public class GFG { public static void main(String[] args) throws Exception { // create user object User user = new User(); // print value of isActive System.out.println("Value before " + "applying setBoolean is " + user.isActive); // Get the marks field object Field field = User.class .getField("isActive"); // Apply setBoolean Method field.setBoolean(field, false); // print result System.out.println("Value after " + "applying setBoolean is " + user.isActive); } } // sample User class class User { // static boolean values public static boolean isActive = true; } |
Value before applying setBoolean is true Value after applying setBoolean is false
Program 2:
// Java program to illustrate setBoolean() method import java.lang.reflect.Field; public class GFG { public static void main(String[] args) throws Exception { // create user object Employee emp = new Employee(); // print value of isManager System.out.println("Value of isManager before " + "applying setBoolean is " + emp.isManager); // Get the marks field object Field field = Employee.class .getField("isManager"); // Apply setBoolean Method field.setBoolean(emp, false); // print value of isActive System.out.println("Value of isPresent before " + "applying setBoolean is " + emp.isManager); // print value of isManager System.out.println("Value of isManager before " + "applying setBoolean is " + emp.isPresent); // Get the marks field object field = Employee.class .getField("isPresent"); // Apply setBoolean Method field.setBoolean(emp, true); // print value of isActive System.out.println("Value of isPresent before " + "applying setBoolean is " + emp.isPresent); } } // sample User class class Employee { // static boolean values public static boolean isPresent = false; public static boolean isManager = true; } |
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-
Recommended Posts:
- Array setBoolean() Method in Java with Examples
- Field set() method in Java with Examples
- Field get() method in Java with Examples
- Field setChar() method in Java with Examples
- Field setInt() method in Java with Examples
- Field setDouble() method in Java with Examples
- Field setFloat() method in Java with Examples
- Field getName() method in Java with Examples
- Field getShort() method in Java with Examples
- Field setLong() method in Java with Examples
- Field setByte() method in Java with Examples
- Field getDeclaredAnnotations() method in Java With Examples
- Field getModifiers() method in Java with Examples
- Field getGenericType() method in Java with Examples
- Field getAnnotationsByType() method in Java With Examples
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

