Open In App

Constructor isSynthetic() method in Java with Examples

Last Updated : 29 Oct, 2019
Summarize
Comments
Improve
Suggest changes
Share
Like Article
Like
Report
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: Java
// 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();
    }
    }
Output:
isSynthetic : false
Program 2: Java
// 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);
        }
    }
}
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()

Next Article

Similar Reads