Java Field getBoolean()方法及示例

来源:这里教程网 时间:2026-02-17 20:50:40 作者:

Java Field getBoolean()方法及示例

java.lang.reflect .Field 类的 getBoolean() 方法是用来获取一个类中包含的静态或实例布尔字段的值。当一个类包含一个静态或实例布尔字段,并且我们想获得该字段的值时,我们可以使用这个方法来返回字段的值。

语法

public boolean getBoolean(Object obj)               throws IllegalArgumentException,                      IllegalAccessException

参数: 该方法接受一个单参数 obj ,它是要提取布尔值的对象。

返回值: 该方法返回所需 布尔字段 的值 。

异常: 该方法会抛出以下异常。

    IllegalAccessException: 如果Field对象正在执行Java语言的访问控制,而底层字段是不可访问的,就会抛出这个异常。IllegalArgumentException: 如果指定的对象不是声明底层字段的类或接口的实例,或者如果字段值不能通过拓扑转换为布尔类型,就会抛出这个异常。NullPointerException: 如果指定的对象是空的,并且该字段是一个实例字段,就会抛出这个异常。ExceptionInitializerError: 如果该方法引发的初始化失败,则抛出该异常。

以下程序说明了getBoolean()方法:

程序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 classclass 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;    }}

输出:

布尔字段isEmployee的值为真
布尔字段isActive的值为假

程序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;        }    }}

输出:

值:false

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

相关推荐