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


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


java.lang.reflect.Field的isSynthetic()方法用于检查Field Object是否为合成字段。如果该字段是合成字段,则该函数返回true,否则它将返回false。

合成构造:合成构造是Java编译器为内部目的而创建的类,字段和方法。

用法:


public boolean isSynthetic()

参数:此方法接受不接受任何内容。

返回:仅当此字段是Java语言规范定义的综合字段时,此方法才返回true。

以下示例程序旨在说明isSynthetic()方法:
示例1:

// Java program to illustrate isSynthetic() method 
  
import java.lang.reflect.Field; 
import java.time.Month; 
  
public class GFG { 
  
    public static void main(String[] args) 
        throws Exception 
    { 
  
        // Get field object 
        Field field 
            = Numbers.class.getField("value"); 
  
        // check field is synthetic or not 
        System.out.println( 
            "The Field is isSynthetic: "
            + field.isSynthetic()); 
    } 
} 
  
// sample Numbers class 
class Numbers { 
  
    // static short value 
    public static long value = 3114256; 
}
输出:
The Field is isSynthetic: false

示例2:

// Java program to illustrate isSynthetic() method 
  
import java.lang.reflect.Field; 
import java.time.DayOfWeek; 
  
public class GFG { 
  
    public static void main(String[] args) 
        throws Exception 
    { 
  
        // Get field object of Month class 
        Field[] fields 
            = DayOfWeek.class
                  .getDeclaredFields(); 
  
        for (int i = 0; i < fields.length; i++) { 
  
            // print name of Fields 
            System.out.println( 
                "The Field "
                + fields[i].toString() 
                + "\n is isSynthetic:"
                + fields[i].isSynthetic()); 
        } 
    } 
}
输出:

The Field public static final java.time.DayOfWeek java.time.DayOfWeek.MONDAY
is isSynthetic:false
The Field public static final java.time.DayOfWeek java.time.DayOfWeek.TUESDAY
is isSynthetic:false
The Field public static final java.time.DayOfWeek java.time.DayOfWeek.WEDNESDAY
is isSynthetic:false
The Field public static final java.time.DayOfWeek java.time.DayOfWeek.THURSDAY
is isSynthetic:false
The Field public static final java.time.DayOfWeek java.time.DayOfWeek.FRIDAY
is isSynthetic:false
The Field public static final java.time.DayOfWeek java.time.DayOfWeek.SATURDAY
is isSynthetic:false
The Field public static final java.time.DayOfWeek java.time.DayOfWeek.SUNDAY
is isSynthetic:false
The Field private static final java.time.DayOfWeek[] java.time.DayOfWeek.ENUMS
is isSynthetic:false
The Field private static final java.time.DayOfWeek[] java.time.DayOfWeek.$VALUES
is isSynthetic:true

参考文献: https://docs.oracle.com/javase/8/docs/api/java/lang/reflect/Field.html#isSynthetic–java



相关用法


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