Close

Java Reflection - Method.getReturnType() Examples

Java Reflection Java Java API 


Class:

java.lang.reflect.Method

java.lang.Objectjava.lang.Objectjava.lang.reflect.AccessibleObjectjava.lang.reflect.AccessibleObjectjava.lang.reflect.AnnotatedElementAnnotatedElementjava.lang.reflect.Executablejava.lang.reflect.Executablejava.lang.reflect.MemberMemberjava.lang.reflect.GenericDeclarationGenericDeclarationjava.lang.reflect.Methodjava.lang.reflect.MethodLogicBig

Method:

public Class<?> getReturnType()

Returns a Class object that represents the formal return type of the method represented by this Method object.


Examples


package com.logicbig.example.method;

import java.lang.reflect.Method;
import java.math.BigDecimal;

public class GetReturnTypeExample {

private static BigDecimal getNumber() {return null;}

public static void main(String... args) throws NoSuchMethodException {
Method m = GetReturnTypeExample.class.getDeclaredMethod("getNumber");
Class<?> returnType = m.getReturnType();
System.out.println(returnType.getTypeName());
}
}

Output

java.math.BigDecimal




package com.logicbig.example.method;

import java.lang.reflect.Method;

public class GetReturnTypeExample2 {

private static void process() {}

public static void main(String... args) throws NoSuchMethodException {
Method m = GetReturnTypeExample2.class.getDeclaredMethod("process");
Class<?> returnType = m.getReturnType();
System.out.println(returnType.getTypeName());
}
}

Output

void




See Also