Correct definition of method signature

H

hiwa

JLS 12.3.3
NoSuchMethodError: A symbolic reference has been encountered that refers
to a specific method of a specific class or interface, but the class or
interface does not contain a method of that signature.

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

8.4.2 only mentions name and argument types of method, but NoSuchMethodError
should have the signature definition that involves return type.

Should I only say that is a JLS bug and the correct one is 12.3.3?
 
C

Chris Uppal

hiwa said:
Should I only say that is a JLS bug and the correct one is 12.3.3?

"Signature" is a relatively vague term and is used to mean slightly different
things in different contexts. For instance, in the context of the Java
language, the return type is not usually treated as part of the signature
(because no two methods can have the same name and argument types, the return
type is not needed to distinguish them). However in the context of JVM
bytecodes (and hence of Java's /runtime/ semantics, which is where
NoSuchMethodError comes from), methods are allowed to differ only in return
type. E.g you can have the bytecode equivalent of:
class SomeClass
{
void aMethod() {}
int aMethod() {return 0;}
}
and no problems occur, since the return type of a method is considered to be
part of its signature. Since method calls in JVM bytecode always name the
method, the argument types, /and/ the return type, there is never any
ambiguity. (Incidentally something similar happens for fields too -- it is
possible to have two fields of the same name provided they have different
types).

-- chris
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,999
Messages
2,570,247
Members
46,844
Latest member
JudyGvh32

Latest Threads

Top