105

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?

1
  • 7
    By the way, there was a bug in the handling of generics in Java 6 which allowed you to have have both methods, as the JVM does use the return type in the signature and call them selectively. This was fixed in Java 7. vanillajava.blogspot.co.uk/2011/02/… Apr 22, 2013 at 20:14

14 Answers 14

152

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.

4
  • So you mean we cannot have two methods in class having same method name, same parameters with different return types? Jun 30, 2016 at 15:47
  • 6
    @KasunSiyambalapitiya of cource we can't. How would the compiler know which of the methods to call in a scenario like this foo.bar(baz);?
    – Kolyunya
    Sep 12, 2016 at 11:53
  • @Jops, what if we have the throws keyword? Does it belong to the signature too?
    – Agent47
    May 13, 2019 at 14:03
  • Is there a formal name for method signature + return type? I assumed it to be "prototype", but probably I'm wrong...
    – davioooh
    May 13, 2021 at 8:46
21

Is class method signature in Java includes return type ?

In Java, it doesn't but in this JVM it does which can lead to obvious confusion.

Is interface method signature in Java includes return type ?

The same as for class methods.

Or only method name and parameters list ?

Method name and parameter types for Java. For example, the parameter annotations and names don't matter.

4
  • 1
    What do you mean by "In Java it doesn't but in JVM it does.". Could you elaborate on how in JVM? Apr 12, 2017 at 7:39
  • 3
    @TarunMaganti The JVM includes the return type in the method signature. Java as a language doesn't. Apr 12, 2017 at 14:05
  • 3
    @xyz this is something you can see by reading the byte code but not the Java code. Any byte code shows this. Mar 6, 2018 at 19:01
  • Is there a formal name for method signature + return type? I assumed it to be "prototype", but probably I'm wrong...
    – davioooh
    May 13, 2021 at 8:46
9

At bytecode level, "return type" is part of method signature. Consider this

public class Test1  {
    public Test1 clone() throws CloneNotSupportedException {
        return (Test1) super.clone();
    }
}

in bytecode there are 2 clone() methods

public clone()LTest1; throws java/lang/CloneNotSupportedException 

public clone()Ljava/lang/Object; throws java/lang/CloneNotSupportedException 

they differ only by return type.

1
  • 1
    this is misleading as instance method implicitly has the instance as first parameter. Once may think that o.m(a) is in fact m(o, a). As such in case of clone, what make difference is the argument not return type.
    – Huy Le
    Oct 10, 2015 at 4:38
7

No not in Java. Method name and parameter list is only for method signature. Return type doesn't include.

7

In JAVA and many other languages, you can call a method without a variable to hold the return value. If return type is part of a method signature, there is no way to know which method will be called when calling without specifying variable holding return value.

7

Java Language Spec says

Two methods have the same signature if they have the same name and argument types.

thus No, return type is not part of method signature.

4

Bro, In java, we use to call methods by their name and their parameters only to use them in our code, like

myMethod(20, 40)

so, JAVA only searches for similar stuff matching in their corresponding declaration (name + param), this is why method signature only includes method's name and parameters. :)

3

The method signature is the name and parameter list only.

3

no, in Java the method signature doesn't includes the return type, but the declaration does.

public             String         getString(String myString)

^access modifier   ^return type   ^name    ^parameter type and name

edited based on feedback below :)

6
  • 1
    That is not what the JLS says. It is "the same name and argument types." The access modifier and parameter name isn't part of the method signature either. Apr 22, 2013 at 14:14
  • if it's a test question that's fine, but if i'm writing a program i'm not writing public getString(), i'm writing public String getString() Apr 22, 2013 at 14:16
  • 1
    The access modifier, return type and throws type(s) are not part of the signature, which is why you can't have String method( String s ) and Double method( String s ) in the same class, for instance. Apr 22, 2013 at 14:16
  • 2
    Perhaps you are confusing method signature with method declaration Apr 22, 2013 at 14:18
  • @Ray i'd like to note, i wrote my answer before he edited the initial question, all he asked was is it part of the signature, i wanted to make sure he wasn't trying to write public name() without listing the return type (truth be told, he could have answered his own question just by writing a simple program to test it) Apr 22, 2013 at 14:18
2

Return type doesn't include in method signature.Only method name and Parameters are defined as method signature.

Reffer : Oracle Docs 'Defining Methods'

2

THE METHOD SIGNATURE INCLUDES THE RETURN TYPE.

The compiler ignores it when has to check for duplicates. For Java is illegal to have two methods with the signature differing only by the return type.

Try that:

public class Called {
    public String aMethod() {
        return "";
    }
}

public class Caller {
    public static void main(String[] main) {
        aMethod();
    }
    public static void aMethod() {
        Called x = new Called();
        x.aMethod();
    }
}

Build the project, go to bin directory, copy the Caller.cass somewhere. Then change the called method:

public int aMethod() {
    return 0;
}

Build the project, you will see that both Called.class and Caller.class have a new timestamp. Replace the Caller.class above and run the project. You'll have an exception:

java.lang.NoSuchMethodError: it.prova.Called.aMethod()Ljava/lang/String;
1

Using AspectJ (org.aspectj.lang.reflect.MethodSignature), it does have the return type

0

The method signature is only the method's name and parameters. But I believe your example will generate an error if they were to be in the same class. You can simply test it out on any ide and see that the compiler will throw an error

0

If you try to run the code you have mentioned on eclipse, you will have an answer as to what elements does java compiler looks for differentiating among java methods :

class Foo {
    public int  myMethod(int param) {
        return param;}
    public char *myMethod*(int param) { //this line throws an error 
        return param;
    }
}

The error thrown is : Duplicate method myMethod(int) in type Foo .

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.