The getBoolean() method of java.lang.reflect.Field class is used to get the value of a static or instance Boolean field contains in a class. When a class contains a static or instance Boolean field and we want to get the value of that field then we can use this method to return the value of Field.
public boolean getBoolean(Object obj) throws IllegalArgumentException, IllegalAccessException
Parameters: This method accepts a single parameter obj which is the object to extract the boolean value from.
Return value: This method returns the value of the required boolean field.
Exception: This method throws following Exception:
- IllegalAccessException: This exception is thrown if Field object is enforcing Java language access control and the underlying field is inaccessible.
- IllegalArgumentException: This exception is thrown if the specified object is not an instance of the class or interface declaring the underlying field or if the field value cannot be converted to the type boolean by a widening conversion.
- NullPointerException: This exception is thrown if the specified object is null and the field is an instance field.
- ExceptionInInitializerError: This exception is thrown if the initialization provoked by this method fails.
Below programs illustrate getBoolean() method:
Value of Boolean Field isEmployee is true
Value of Boolean Field isActive is false
Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.