Search code examples
javaprogramming-languagesreturnoverloading

Java - why no return type based method overloading?


I know this is not possible but can anyone provide a theory as to why Java chose not to support this? I am asking because I just ran into a situation where I think it would be nice to have.


Solution

  • Because you are not required to capture the return value of a method in Java, in which case the compiler can not decide which overload to use. E.g.

    boolean doSomething() { ... }
    
    int doSomething() { ... }
    
    doSomething(); // which one to call???