Constructor isSynthetic() method in Java with Examples
Last Updated :
29 Oct, 2019
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:
import java.lang.reflect.Constructor;
public class GFG {
public static void main(String[] args)
{
Class classObj = shape. class ;
Constructor[] cons = classObj.getConstructors();
boolean answer = cons[ 0 ].isSynthetic();
System.out.println( "isSynthetic : " + answer);
}
@CustomAnnotation (createValues = "GFG" )
public class shape {
@CustomAnnotation (createValues = "GFG" )
public shape()
{
}
}
public @interface CustomAnnotation {
public String createValues();
}
}
|
Output:
isSynthetic : false
Program 2:
import java.lang.annotation.Annotation;
import java.lang.reflect.Constructor;
public class GFG {
public static void main(String[] args)
{
Class classObj = String. class ;
Constructor[] cons = classObj.getConstructors();
for ( int i = 0 ; i < cons.length; i++) {
System.out.println( "Constructor: "
+ cons[i]);
boolean answer = cons[ 0 ].isSynthetic();
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()
Share your thoughts in the comments
Please Login to comment...