当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Java Field getBoolean()用法及代码示例


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

用法:

public boolean getBoolean(Object obj)
               throws IllegalArgumentException,
                      IllegalAccessException

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


返回值:此方法返回必需的布尔字段的值。

异常:此方法引发以下异常:

  1. IllegalAccessException:如果Field对象正在强制执行Java语言访问控制并且基础字段不可访问,则抛出此异常。
  2. IllegalArgumentException:如果指定的对象不是声明基础字段的类或接口的实例,或者如果不能通过扩展转换将字段值转换为布尔类型,则抛出此异常。
  3. NullPointerException :如果指定对象为null并且该字段是实例字段,则抛出此异常。
  4. ExceptionInInitializerError:如果此方法引发的初始化失败,则抛出此异常。

以下示例程序旨在说明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 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; 
    } 
}
输出:

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

示例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; 
        } 
    } 
}
输出:

Value: false

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



相关用法


注:本文由纯净天空筛选整理自AmanSingh2210大神的英文原创作品 Field getBoolean() method in Java with Examples。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。