How to find if a method returns void?

Normally this is the question which comes when using reflection to find the return type of a method. Void class comes to rescue.

   public class VoidClassApp {
       public static void main(String[] args) throws NoSuchMethodException {
          Class clazz = Test.class.getMethod("method").getReturnType();
          System.out.println(clazz == Void.TYPE);
       }
   }

   class Test {
      public void method() {
         System.out.println("Does Nothing");
      }
  }

The output is : true

Advertisement

Posted on April 27, 2011, in java and tagged . Bookmark the permalink. Leave a comment.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: