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

JavaMethod Signature

Java Problem Overview


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?

Java Solutions


Solution 1 - Java

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.

Solution 2 - Java

> 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.

Solution 3 - Java

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.

Solution 4 - Java

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.

Solution 5 - Java

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

Solution 6 - Java

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.

Solution 7 - Java

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. :)

Solution 8 - Java

The method signature is the name and parameter list only.

Solution 9 - Java

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 :)

Solution 10 - Java

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

Reffer : Oracle Docs 'Defining Methods'

Solution 11 - Java

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

Solution 12 - Java

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;

Solution 13 - Java

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

Solution 14 - Java

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 .

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionfaressoftView Question on Stackoverflow
Solution 1 - JavaJopsView Answer on Stackoverflow
Solution 2 - JavaPeter LawreyView Answer on Stackoverflow
Solution 3 - JavaEvgeniy DorofeevView Answer on Stackoverflow
Solution 4 - JavaPermGenErrorView Answer on Stackoverflow
Solution 5 - Javauser3580271View Answer on Stackoverflow
Solution 6 - JavaHuy LeView Answer on Stackoverflow
Solution 7 - JavaNIKHIL CHAURASIAView Answer on Stackoverflow
Solution 8 - JavaRay StojonicView Answer on Stackoverflow
Solution 9 - JavaJeff HawthorneView Answer on Stackoverflow
Solution 10 - JavaKandyView Answer on Stackoverflow
Solution 11 - JavadevnullView Answer on Stackoverflow
Solution 12 - JavaLucio MenciView Answer on Stackoverflow
Solution 13 - Javalest96View Answer on Stackoverflow
Solution 14 - JavaNishant_SinghView Answer on Stackoverflow