Java Reflection method get return type

Introduction

The return type of a method may be obtained using either of the following methods:

public Class<?> getReturnType()  
public Type getGenericReturnType()  

The former returns a Class and the latter returns a Type representing the return type.


import java.lang.reflect.Method;
import java.lang.reflect.Type;

public class Main {
   public static void main(String args[]) throws Exception {

      Class c = Class.forName("java.lang.String");
      Method[] methods = c.getDeclaredMethods();
      for (Method m : methods) {
         System.out.println("\n\nMethod: " + m.getName());
         System.out.println("Return type: " + m.getGenericReturnType());
         //from   w  w  w.ja  v a2  s.c o  m
      }
   }
}



PreviousNext

Related