Search code examples
javamethod-signature

Does a method's signature in Java include its return type?


Does the method signature in a Java class/interface include its return type?

Example:

Does Java know the difference between those two methods:

public class Foo {
    public int  myMethod(int param) {}
    public char myMethod(int param) {}
}

Or is it maybe only the method name and parameters list that matter?


Solution

  • Quoting from Oracle Docs:

    Definition: Two of the components of a method declaration comprise the method signature—the method's name and the parameter types.

    enter image description here

    Since the question was edited to include this example:

    public class Foo {
        public int  myMethod(int param) {}
        public char myMethod(int param) {}
    }
    

    No, the compiler won't know the difference, as their signature: myMethod(int param) is the same. The second line:

        public char myMethod(int param) {}
    

    will give you can error: method is already defined in class, which further confirms the above statement.