Field isSynthetic() method in Java with Examples
Last Updated :
30 May, 2022
The isSynthetic() method of java.lang.reflect.Field is used to check whether Field Object is a synthetic field or not. If the field is a synthetic field then the function returns true otherwise it will return false. Synthetic Construct: Synthetic Construct is Class, Fields, and Methods that are created by the Java compiler for internal purposes. Syntax:
public boolean isSynthetic()
Parameters: This method accepts nothing. Return: This method returns true if and only if this field is a synthetic field as defined by the Java Language Specification. Below programs illustrate isSynthetic() method: Program 1:
Java
import java.lang.reflect.Field;
import java.time.Month;
public class GFG {
public static void main(String[] args)
throws Exception
{
Field field
= Numbers. class .getField("value");
System.out.println(
"The Field is isSynthetic: "
+ field.isSynthetic());
}
}
class Numbers {
public static long value = 3114256 ;
}
|
Output:
The Field is isSynthetic: false
Program 2:
Java
import java.lang.reflect.Field;
import java.time.DayOfWeek;
public class GFG {
public static void main(String[] args)
throws Exception
{
Field[] fields
= DayOfWeek. class
.getDeclaredFields();
for ( int i = 0 ; i < fields.length; i++) {
System.out.println(
"The Field "
+ fields[i].toString()
+ "\n is isSynthetic:"
+ fields[i].isSynthetic());
}
}
}
|
Output: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
References: https://docs.oracle.com/javase/8/docs/api/java/lang/reflect/Field.html#isSynthetic–java
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...