Thursday, June 11, 2009

Java methods and declared return type

Guess the output:

May seem straight forward to Java Geeks.

I thought this would give compile time error. Later on realized the output is

This is funny

Moral: It is possible for a method to have a declared return type and yet contain no return statements.


  1. Funny, interesting, cool.

    BTW, do you know that you can instantiate an object in C++ in the following ways?

    MyClass obj = new MyClass();
    MyClass obj = new MyClass;

    Now, tell me. What's the difference?

    PS: The answer is here.

  2. @Srikanth Thanks. I too didnt know that.