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 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.
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:
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.
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.
Synthetic Methods of BigInteger Class are Method: compareTo
Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.