The Wayback Machine - https://web.archive.org/web/20240802231452/https://www.geeksforgeeks.org/field-issynthetic-method-in-java-with-examples/
Open In App

Field isSynthetic() method in Java with Examples

Last Updated : 30 May, 2022
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report

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




// 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;
}


Output:

The Field is isSynthetic: false

Program 2: 

Java




// 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());
        }
    }
}


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



Similar Reads

Constructor isSynthetic() method in Java with Examples
The isSynthetic() method of java.lang.reflect.Constructor class is used to return the boolean value true if this constructor object is a synthetic construct else method returns false showing this construct is not a synthetic construct. As we know Synthetic Constructs are Class, Fields, and Methods that are created by the Java compiler for internal
3 min read
Class isSynthetic() method in Java with Examples
The isSynthetic() method of java.lang.Class class is used to check if this Class is the Synthetic class. The method returns true if this Class is the Synthetic class. It returns false otherwise.Syntax: public boolean isSynthetic() Parameter: This method does not accept any parameter.Return Value: This method returns true if this Class is the Synthe
2 min read
Method class isSynthetic() method in Java
java.lang.reflectMethod class help us to get information of a single method on a class or interface. This class also provides access to the methods of classes and invoke them at runtime. isSynthetic() method of Method class: This function checks whether Method Object is a synthetic construct or not. If the method is a synthetic construct then the f
3 min read
Field getAnnotatedType() method in Java With Examples
The getAnnotatedType() method of java.lang.reflect.Field is used to return an annonatedType object that represents the use of a type to specify the declared type of the field. Every Field of the class is declared by some AnnotatedType.AnnotatedType represents the potentially annotated use of any type including an array type, a parameterized type, a
2 min read
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) throw
3 min read
Field equals() method in Java with Examples
The equals() method of java.lang.reflect.Field is used to compare two field objects. This method compares two field objects and returns true if both objects are equal otherwise false. The two Field objects are considered equal if and only if when they were declared by the same class and have the same name and type. This method is very helpful at th
3 min read
Field getDeclaringClass() method in Java with Examples
The getDeclaringClass() method of java.lang.reflect.Field is used to get the Class object which declares the field represented by this Field object. If Field object is present and we want to get the class object then we can get that class object using this method. Syntax: public Class&lt;T&gt; getDeclaringClass() Parameters: This method accepts not
2 min read
Field getChar() method in Java with Examples
The getChar() method of java.lang.reflect.Field is used to get the value of char which has to be static or instance field type. This method also used to get the value of another primitive type convertible to type char via a widening conversion. When a class contains a static or instance Char field and we want to get the value of that field then we
3 min read
Field getFloat() method in Java with Examples
The getFloat() method of java.lang.reflect.Field used to get the value of float which has to be static or instance field type. This method also used to get the value of another primitive type convertible to type float via a widening conversion. When a class contains a static or instance float field and we want to get the value of that field then we
3 min read
Field getDouble() method in Java with Examples
The getDouble() method of java.lang.reflect.Field used to get the value of double which has to be static or instance field type. This method also used to get the value of another primitive type convertible to type double via a widening conversion. When a class contains a static or instance double field and we want to get the value of that field the
3 min read
three90RightbarBannerImg