Method Class | toGenericString() method in Java

The java.lang.reflect.Method.toGenericString() method of Method class returns a string which gives the details of Method, including details of type parameters of the method.

Syntax:

public String toGenericString()

Return Value: This method returns a string which gives the details of Method, including details of type parameters of the method.

Below programs illustrates toGenericString() method of Method class:

Example 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Program Demonstrate toGenericString() method
// of Method Class.
  
import java.lang.reflect.Method;
  
public class GFG {
  
    // create another method
    public final void paint(Object... values)
    {
        String message = "A Computer Science portal for geeks";
    }
  
    // create main method
    public static void main(String args[])
    {
  
        try {
  
            // get list of declared method objects of class GFG
            Method[] methods = GFG.class.getMethods();
  
            // loop through method list
            for (Method method : methods) {
  
                // print only for main and paint methods
                if (method.getName().equals("main")
                    || method.getName().equals("paint")) {
  
                    // print method details using toGenericString()
                    System.out.println(method.toGenericString());
                }
            }
        }
        catch (Exception e) {
  
            // Print Exception if any Exception occurs
            e.printStackTrace();
        }
    }
}

chevron_right


Output:

public static void GFG.main(java.lang.String[])
public final void GFG.paint(java.lang.Object...)

Example 2:

Explanation: In this Method at first java.util.concurrent.CountDownLatch Class Object is created. After creating Class Object of java.util.concurrent.CountDownLatch Class a list of Method Objects is created by calling getMethods() of class Object. Iterate through Method list and print method details using toGenericString().

filter_none

edit
close

play_arrow

link
brightness_4
code

// Program Demonstrate toGenericString() method
// of Method Class.
  
import java.lang.reflect.Method;
import java.util.concurrent.CountDownLatch;
public class GFG {
  
    // create main method
    public static void main(String args[])
    {
  
        try {
  
            // create class object for class CountDownLatch
            Class c = CountDownLatch.class;
  
            // get list of Method object
            Method[] methods = c.getMethods();
  
            System.out.println("Methods of CountDownLatch: ");
            // Loop through Methods list
            for (Method m : methods) {
  
                // Print Method details
                System.out.println("Method: "
                                   + m.toGenericString());
            }
        }
        catch (Exception e) {
            // print Exception is any Exception occurs
            e.printStackTrace();
        }
    }
}

chevron_right


Output:

Methods of CountDownLatch: 
Method: public boolean java.util.concurrent.CountDownLatch.await(long,java.util.concurrent.TimeUnit) throws java.lang.InterruptedException
Method: public void java.util.concurrent.CountDownLatch.await() throws java.lang.InterruptedException
Method: public void java.util.concurrent.CountDownLatch.countDown()
Method: public long java.util.concurrent.CountDownLatch.getCount()
Method: public java.lang.String java.util.concurrent.CountDownLatch.toString()
Method: public final void java.lang.Object.wait(long,int) throws java.lang.InterruptedException
Method: public final native void java.lang.Object.wait(long) throws java.lang.InterruptedException
Method: public final void java.lang.Object.wait() throws java.lang.InterruptedException
Method: public boolean java.lang.Object.equals(java.lang.Object)
Method: public native int java.lang.Object.hashCode()
Method: public final native java.lang.Class java.lang.Object.getClass()
Method: public final native void java.lang.Object.notify()
Method: public final native void java.lang.Object.notifyAll()

Explanation: Output of this program also showing results for method objects other than methods defined in class CountDownLatch like wait, equals, toString, hashCode, getClass, notify, notifyAll.These methods are inherited from superclass name Object of java.lang package.

Difference between toGenericString() and toString()

  • toGenericString() returns a string describing this Method, including its generic type parameters.
  • toString() returns a string describing this Method.
  • toString() method does not include generic types.

Reference:



My Personal Notes arrow_drop_up

I am a Developer I love to code and bring my ideas alive

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.