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 purposes. In the case of Constructor, the synthetic construct is the constructor created by java compiler for internal uses. This method is helpful for identifying those constructs.

Syntax:

public boolean isSynthetic()

Parameters: This method accepts nothing.



Return: This method returns true if and only if this executable is a synthetic construct as defined by The Java Language Specification.

Below programs illustrate isSynthetic() method:
Program 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to illustrate isSynthetic() method
  
import java.lang.reflect.Constructor;
  
public class GFG {
  
    public static void main(String[] args)
    {
        // create a class object
        Class classObj = shape.class;
  
        // get Constructor object
        // array from class object
        Constructor[] cons = classObj.getConstructors();
  
        // check isSynthetic or not
        boolean answer = cons[0].isSynthetic();
  
        // print result
        System.out.println("isSynthetic : " + answer);
    }
  
    @CustomAnnotation(createValues = "GFG")
    public class shape {
  
        @CustomAnnotation(createValues = "GFG")
        public shape()
        {
        }
    }
  
    // create a custom annotation
    public @interface CustomAnnotation {
        public String createValues();
    }
    }

chevron_right


Output:

isSynthetic : false

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to illustrate isSynthetic() method
  
import java.lang.annotation.Annotation;
import java.lang.reflect.Constructor;
  
public class GFG {
  
    public static void main(String[] args)
    {
        // create a class object
        Class classObj = String.class;
  
        // get Constructor object
        // array from class object
        Constructor[] cons = classObj.getConstructors();
  
        for (int i = 0; i < cons.length; i++) {
  
            System.out.println("Constructor: "
                               + cons[i]);
  
            // get array of isSynthetic
            boolean answer = cons[0].isSynthetic();
  
            // print result
            System.out.println("isSynthetic : " + answer);
        }
    }
}

chevron_right


Output:

Constructor: public java.lang.String(byte[], int, int)
isSynthetic : false
Constructor: public java.lang.String(byte[], java.nio.charset.Charset)
isSynthetic : false
Constructor: public java.lang.String(byte[], java.lang.String) throws java.io.UnsupportedEncodingException
isSynthetic : false
Constructor: public java.lang.String(byte[], int, int, java.nio.charset.Charset)
isSynthetic : false
Constructor: public java.lang.String(byte[], int, int, java.lang.String) throws java.io.UnsupportedEncodingException
isSynthetic : false
Constructor: public java.lang.String(java.lang.StringBuilder)
isSynthetic : false
Constructor: public java.lang.String(java.lang.StringBuffer)
isSynthetic : false
Constructor: public java.lang.String(byte[])
isSynthetic : false
Constructor: public java.lang.String(int[], int, int)
isSynthetic : false
Constructor: public java.lang.String()
isSynthetic : false
Constructor: public java.lang.String(char[])
isSynthetic : false
Constructor: public java.lang.String(java.lang.String)
isSynthetic : false
Constructor: public java.lang.String(char[], int, int)
isSynthetic : false
Constructor: public java.lang.String(byte[], int)
isSynthetic : false
Constructor: public java.lang.String(byte[], int, int, int)
isSynthetic : false

References: https://docs.oracle.com/javase/10/docs/api/java/lang/reflect/Constructor.html#isSynthetic()



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.