Method Class | getReturnType() Method in Java
Last Updated :
12 Nov, 2021
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
import java.lang.reflect.Method;
public class GFG {
public static void main(String[] args)
{
try {
Class classobj = demoForReturnParam. class ;
Method[] methods = classobj.getMethods();
for (Method method : methods) {
if (method.getName().equals( "setValue" )
|| method.getName().equals( "getValue" )
|| method.getName().equals( "setManyValues" )) {
Class returnParam = method.getReturnType();
System.out.println( "\nMethod Name : "
+ method.getName());
System.out.println( "Return Type Details: " + returnParam.getName());
}
}
}
catch (Exception e) {
e.printStackTrace();
}
}
}
class demoForReturnParam {
public int setValue()
{
System.out.println( "setValue" );
return 24 ;
}
public String getValue()
{
System.out.println( "getValue" );
return "getValue" ;
}
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
import java.lang.reflect.Method;
public class GFG {
public static void main(String[] args)
{
try {
Class classobj = GFG. class ;
Method[] methods = classobj.getMethods();
for (Method method : methods) {
Class returnParam = method.getReturnType();
System.out.println( "\nMethod Name : "
+ method.getName());
System.out.println( "Return Type Details: " + returnParam.getName());
}
}
catch (Exception e) {
e.printStackTrace();
}
}
public int method1()
{
System.out.println( "method1" );
return 24 ;
}
public String method2()
{
System.out.println( "method2" );
return "method3" ;
}
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()
Share your thoughts in the comments
Please Login to comment...