Open In App

Method Class | getReturnType() Method in Java

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Share
Report issue
Report

Prerequisite : Java.lang.Class class in Java | Set 1, Java.lang.Class class in Java | Set 2
The java.lang.reflectMethod Class help in getting 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. 
The getReturnType() method of Method class 
Every Method has a return type whether it is void, int, double, string or any other datatype. The getReturnType() method of Method class returns a Class object that represent the return type, declared in method at time of creating the method.
Syntax: 
 

public Class<?> getReturnType()

Parameters: The method does not take any parameters.
Return Value: The method returns a Class object that represent the formal return type of the method object.
Below programs illustrate the getReturnType() method of Method class :
Program 1: Below program prints Return Type for some specific methods of a class provided as input in main method of program. 
 

Java




/*
* Program Demonstrate how to apply getReturnType() method
* of Method Class.
*/
import java.lang.reflect.Method;
 
public class GFG {
 
    // Main method
    public static void main(String[] args)
    {
        try {
            // Create class object
            Class classobj = demoForReturnParam.class;
 
            // Get Method Object
            Method[] methods = classobj.getMethods();
 
            // Iterate through methods
            for (Method method : methods) {
 
                // We are only taking method defined in the demo class
                // We are not taking other methods of the object class
                if (method.getName().equals("setValue")
                    || method.getName().equals("getValue")
                    || method.getName().equals("setManyValues")) {
                    // apply getReturnType() method
                    Class returnParam = method.getReturnType();
 
                    // print return Type class object of method Object
                    System.out.println("\nMethod Name : "
                                       + method.getName());
 
                    System.out.println("Return Type Details: " + returnParam.getName());
                }
            }
        }
        catch (Exception e) {
            e.printStackTrace();
        }
    }
}
 
// A simple class
class demoForReturnParam {
 
    // Method returning int value
    public int setValue()
    {
        System.out.println("setValue");
        return 24;
    }
 
    // Method returning string value
    public String getValue()
    {
        System.out.println("getValue");
        return "getValue";
    }
 
    // Method returning nothing
    public void setManyValues(int value1, String value3)
    {
        System.out.println("setManyValues");
    }
}


Output: 

Method Name : setManyValues
Return Type Details: void

Method Name : getValue
Return Type Details: java.lang.String

Method Name : setValue
Return Type Details: int

 

Program 2: Below program prints Return Type for all the methods of a class provided in main method of program. 
 

Java




/*
* Program Demonstrate how to apply getReturnType() method
* of Method Class.
*/
import java.lang.reflect.Method;
 
public class GFG {
 
    // Main method
    public static void main(String[] args)
    {
        try {
            // Create class object
            Class classobj = GFG.class;
 
            // Get Method Object
            Method[] methods = classobj.getMethods();
 
            // Iterate through methods
            for (Method method : methods) {
 
                // Apply getReturnType() method
                Class returnParam = method.getReturnType();
 
                // Print return Type class object of method Object
                System.out.println("\nMethod Name : "
                                   + method.getName());
 
                System.out.println("Return Type Details: " + returnParam.getName());
            }
        }
        catch (Exception e) {
            e.printStackTrace();
        }
    }
    // Method returning int value
    public int method1()
    {
        System.out.println("method1");
        return 24;
    }
 
    // Method returning string value
    public String method2()
    {
        System.out.println("method2");
        return "method3";
    }
 
    // Method returning nothing
    public void method3(int value1, String value3)
    {
        System.out.println("method3");
    }
}


Output: 

Method Name : method3
Return Type Details: void

Method Name : method2
Return Type Details: java.lang.String

Method Name : method1
Return Type Details: int

Method Name : main
Return Type Details: void

Method Name : wait
Return Type Details: void

Method Name : wait
Return Type Details: void

Method Name : wait
Return Type Details: void

Method Name : equals
Return Type Details: boolean

Method Name : toString
Return Type Details: java.lang.String

Method Name : hashCode
Return Type Details: int

Method Name : getClass
Return Type Details: java.lang.Class

Method Name : notify
Return Type Details: void

Method Name : notifyAll
Return Type Details: void

 

Explanation: Output of this program also showing results for method objects other than methods defined in class object like wait, equals, toString, hashCode, getClass, notify, notifyAll. These methods are inherited from super class name Object of java.lang lang package by class object.
Reference: 
Oracle Doc for getReturnType()
 



Last Updated : 12 Nov, 2021
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads