views:

252

answers:

4

Hi,

I have a java.lang.reflect.Method object and I would like to know if it's return type is void.

I've checked the Javadocs and there is a getReturnType() method that returns a Class object. The thing is that they don't say what would be the return type if the method is void.

Thanks!

A: 

See Void class on Javadoc.

Romain
+12  A: 
if( method.getReturnType().equals(Void.TYPE)){
    out.println("It does");
 }

Quick sample:

$cat X.java  

import java.lang.reflect.Method;


public class X {
    public static void main( String [] args ) {
        for( Method m : X.class.getMethods() ) {
            if( m.getReturnType().equals(Void.TYPE)){
                System.out.println( m.getName()  + " returns void ");
            }
        }
    }

    public void hello(){}
}
$java X
hello returns void 
main returns void 
wait returns void 
wait returns void 
wait returns void 
notify returns void 
notifyAll returns void
OscarRyz
+2  A: 

It returns java.lang.Void.TYPE.

James Keesey
+4  A: 

method.getReturnType() returns void.class/Void.TYPE.

Tom Hawtin - tackline