Sunday, November 17, 2024
Google search engine
HomeLanguagesJavaField getBoolean() method in Java with Examples

Field getBoolean() method in Java with Examples

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.

Syntax:

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:

  1. IllegalAccessException: This exception is thrown if Field object is enforcing Java language access control and the underlying field is inaccessible.
  2. 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.
  3. NullPointerException: This exception is thrown if the specified object is null and the field is an instance field.
  4. ExceptionInInitializerError: This exception is thrown if the initialization provoked by this method fails.

Below programs illustrate getBoolean() method:
Program 1:




// Java program to demonstrate the above method
  
import java.lang.reflect.Field;
  
public class GFG {
  
    public static void main(String[] args)
        throws NoSuchFieldException,
               SecurityException,
               IllegalArgumentException,
               IllegalAccessException
    {
  
        // Create the User class object
        User user = new User();
  
        // Get the isEmployee field object
        Field field = User.class.getField("isEmployee");
  
        // Apply getBoolean Method on User Object
        // to get the value of isEmployee field
        boolean value = field.getBoolean(user);
  
        // print result
        System.out.println("Value of Boolean Field"
                           + " isEmployee is " + value);
  
        // Now Get the isActive field object
        field = User.class.getField("isActive");
  
        // Apply getBoolean Method on User Object
        // to get the value of isActive field
        value = field.getBoolean(user);
  
        // print result
        System.out.println("Value of Boolean Field"
                           + " isActive is " + value);
    }
}
  
// sample User class
class User {
  
    // static boolean values
    public static boolean isEmployee = true;
    public static boolean isActive = false;
  
    public static boolean isEmployee()
    {
        return isEmployee;
    }
    public static void setEmployee(boolean isEmployee)
    {
        User.isEmployee = isEmployee;
    }
    public static boolean isActive()
    {
        return isActive;
    }
    public static void setActive(boolean isActive)
    {
        User.isActive = isActive;
    }
}


Output:

Value of Boolean Field isEmployee is true
Value of Boolean Field isActive is false

Program 2:




// Java program to demonstrate the above method
  
import java.lang.reflect.Field;
  
public class GFG {
  
    public static void main(String[] args)
        throws NoSuchFieldException,
               SecurityException,
               IllegalArgumentException,
               IllegalAccessException
    {
  
        // Create the mathDigit class object
        mathDigit math = new mathDigit();
  
        // Get the isEmployee field object
        Field field = mathDigit.class.getField("isZero");
  
        // Apply getBoolean Method on field Object
        // to get the value of isZero field
        boolean value = field.getBoolean(math);
  
        // print result
        System.out.println("Value: " + value);
    }
  
    // mathDigit class
    static class mathDigit {
  
        public static boolean isZero = false;
        public int number;
  
        public static boolean isZero()
        {
            return isZero;
        }
        public static void setZero(boolean isZero)
        {
            mathDigit.isZero = isZero;
        }
        public int getNumber()
        {
            return number;
        }
        public void setNumber(int number)
        {
            this.number = number;
        }
    }
}


Output:

Value: false

References: https://docs.oracle.com/javase/10/docs/api/java/lang/reflect/Field.html#getBoolean(java.lang.Object)

RELATED ARTICLES

Most Popular

Recent Comments