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

14

107

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?

Mithras answered 22/4, 2013 at 14:10 Comment(1)
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/…Selfcontained
P
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.

Persons answered 22/4, 2013 at 14:17 Comment(4)
So you mean we cannot have two methods in class having same method name, same parameters with different return types?Fraenum
@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);?Mammon
@Jops, what if we have the throws keyword? Does it belong to the signature too?Rucker
Is there a formal name for method signature + return type? I assumed it to be "prototype", but probably I'm wrong...Leporide
S
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.

Selfcontained answered 22/4, 2013 at 14:12 Comment(4)
What do you mean by "In Java it doesn't but in JVM it does.". Could you elaborate on how in JVM?Bonded
@TarunMaganti The JVM includes the return type in the method signature. Java as a language doesn't.Selfcontained
@xyz this is something you can see by reading the byte code but not the Java code. Any byte code shows this.Selfcontained
Is there a formal name for method signature + return type? I assumed it to be "prototype", but probably I'm wrong...Leporide
E
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.

Emmaline answered 22/4, 2013 at 14:22 Comment(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.Melamie
M
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.

Moshemoshell answered 22/4, 2013 at 14:12 Comment(0)
D
7

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

Durbin answered 28/4, 2014 at 7:11 Comment(0)
M
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.

Melamie answered 10/10, 2015 at 4:48 Comment(0)
U
4

The method signature is the name and parameter list only.

Unclear answered 22/4, 2013 at 14:11 Comment(0)
H
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. :)

Headwork answered 1/12, 2014 at 5:18 Comment(0)
K
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 :)

Karen answered 22/4, 2013 at 14:13 Comment(6)
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.Selfcontained
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()Karen
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.Unclear
Perhaps you are confusing method signature with method declarationSelfcontained
@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)Karen
@PeterLawrey probable :) but as i said to Ray, i wrote my answer before the OP editedKaren
X
3

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;
X answered 8/6, 2017 at 8:40 Comment(0)
B
2

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

Reffer : Oracle Docs 'Defining Methods'

Brazilin answered 2/8, 2014 at 4:42 Comment(0)
G
1

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

Gratify answered 26/6, 2013 at 19:12 Comment(0)
V
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

Verdin answered 10/10, 2015 at 5:16 Comment(0)
F
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 .

Furtado answered 2/2, 2018 at 0:1 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.