Method class isSynthetic() method in Java
Last Updated :
06 Apr, 2023
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 function returns true otherwise it will return false.
Synthetic Construct: Synthetic Construct are Class, Fields, and Methods that are created by the Java compiler for internal purposes.
Syntax:
public boolean isSynthetic()
Return Value: This method returns true if and only if Method is a synthetic construct as specified by the JVM. Else it returns false.
Below programs illustrates isSynthetic() method of Method class:
Example 1:
In below program when the main method creates an object of nested private class Demo and tries to access the private variable name “message”. When it is compiled, it will create a synthetic method. The details of that synthetic method can be obtained by getting the object of that method with the use of isSynthetic() method as shown in below program.
import java.lang.reflect.Method;
public class GFG {
private static final class Demo {
private String message = "A Computer Science"
+ " portal for geeks" ;
}
public static void main(String args[])
{
try {
Demo obj = new Demo();
System.out.println( "private Message"
+ " variable of Demo class"
+ obj.message);
Class classobj = obj.getClass();
Method[] methods = classobj.getDeclaredMethods();
for (Method method : methods) {
boolean isSynthetic = method.isSynthetic();
System.out.println(method
+ " method is Synthetic Method :"
+ isSynthetic);
}
}
catch (Exception e) {
e.printStackTrace();
}
}
}
|
Output:
private Message variable of Demo classA Computer Science portal for geeks
static java.lang.String GFG$Demo.access$100(GFG$Demo) method is Synthetic Method :true
Example 2: Program to return all the Synthetic construct Methods of BigInteger class.
Explanation: In this Method, at first, BigInteger Class Object is created. After creating Class Object of BigInteger Class, a list of Method Objects is created by calling getMethods() of class Object. Iterate through Method list and get Synthetic Method by checking Method is Synthetic or not using isSynthetic(). At last print Synthetic Method name.
import java.lang.reflect.Method;
import java.math.BigInteger;
public class GFG {
public static void main(String args[])
{
try {
Class c = BigInteger. class ;
Method[] methods = c.getMethods();
System.out.println( "Synthetic Methods"
+ " of BigInteger Class are" );
for (Method m : methods) {
if (m.isSynthetic()) {
System.out.println( "Method: "
+ m.getName());
}
}
}
catch (Exception e) {
e.printStackTrace();
}
}
}
|
Output:
Synthetic Methods of BigInteger Class are
Method: compareTo
Reference:
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...