jGuru
Register Email     Password Forgot your
password?
HOME FAQS FORUMS DOWNLOADS ARTICLES PEERSCOPE LEARN

  Search   jGuru Search Help

Question What is a method's signature?
Topics Java:Language, Java:Language:Declarations, Java:Language:Semantics, Java:Implementation:VM
Author John Zukowski PREMIUM
Created Feb 18, 2000 Modified Feb 18, 2000


Answer
The signature of a method is the combination of the method's name along with the number and types of the parameters (and their order).

Is this item helpful?  yes  no     Previous votes   Yes: 1  No: 1



Comments and alternative answers

Comment on this FAQ entry

method signature
Josh Downes, Jan 16, 2003  [replies:1]
I was kind of hoping for an example. Is this one:

myMethod(int i, int j);

// does the signature include the ';'?

Is this item helpful?  yes  no     Previous votes   Yes: 0  No: 0



Reply to this answer/comment  Help  
Re: method signature
Anita gorantla, Jan 16, 2003
No it should not include ;.

Is this item helpful?  yes  no     Previous votes   Yes: 0  No: 0



Reply to this answer/comment  Help  
incomplete, thus incorrect
Deepak Kenchamba, Jun 16, 2004  [replies:11]
Specific to java, The signature of a method should contain more details. It should contain
1. Visibility modifier (public, private, protected)
2. Return type
3. Name of the method
4. Arguments (type, order)
5. Throws clause
Deepak Kenchamba.

Is this item helpful?  yes  no     Previous votes   Yes: 2  No: 2



Reply to this answer/comment  Help  
Re: incomplete, thus incorrect
cypher punks, Oct 15, 2004  [replies:6]
Technically, in Java, a method signature consists only of the name of the method and the parameter types and their order. The modifiers, return type and throws clause are not part of the signature.

Furthermore, in response to the first reply where the question was:

myMethod(int i, int j); // does the signature include the ';'?
... the answer is 'not exactly'. A method signature is an abstract concept - not something you can type in anywhere.

I repeat: a method signature doesn't exist in a text file, or in java source - it exists only in your head or in the compiler's "head".

In that sense (and in the sense explicitly given by the Java Language Specification) the original answerer was right and you guys are unfortunately wrong.

Is this item helpful?  yes  no     Previous votes   Yes: 0  No: 0



Reply to this answer/comment  Help  

Why the return type is not included in the method signature?
emil dinchev, Feb 9, 2006  [replies:5]
Look at the following code (it is illegal):
 public class A {
   public Object getSomething() {
     return new Object();
   }
   public String getSomething() {
     return new String();
   }
 }
 A a = new A();
 Object o = a.getSomething();
 String s = a.getSomething();
I'm wondering why this code is not permitted. The compiler can detect the expected return type of the method and to use the correct method during the call.

Is this item helpful?  yes  no     Previous votes   Yes: 0  No: 0



Reply to this answer/comment  Help  
Re: Why the return type is not included in the method signature?
paul t, Apr 3, 2006  [replies:2]
I am not sure what you expect as an answer this code wont work coz you are not obeying the rules of method overloading. In method overloading you need to change the arguments(which in this case is none) but not the return type.....

Is this item helpful?  yes  no     Previous votes   Yes: 0  No: 0



Reply to this answer/comment  Help  
Re[2]: Why the return type is not included in the method signature?
Kamal Chandana Mettananda, Apr 27, 2006  [replies:1]
In method overloading you have to change the method singature while keeping the name unchanged.

There's nothing to do with the return type or throws clause regarding method signature.

As you guys have discussed in above posts, only the parameter types with the order metters.

eg:
public Object doSomething(String param1, Object param2) throws Exception;
public void     doSomething(Object param2, String param1);

What matters is only the parameter types with the order.

Is this item helpful?  yes  no     Previous votes   Yes: 0  No: 0



Reply to this answer/comment  Help  

Re[3]: Why the return type is not included in the method signature?
Eric Sun, Jun 5, 2006
I think Emil's question is a good question. I think Emil knows that signiture does not include return type, but why Java does include return type in signature so that it is more convenient for programmers to do the overload? Let me try to answer here, I am not sure whether it is a good answer:

1. Signiture of a method is an identifier for JVM to identify where to locate the method in memory or steak, so that it can branch to that method. Java uses method name and parameters as identifier. If two method with the same signiture, compiler will say "you are wrong, identifier need to be unique, I don't know where to go if you give two same identifiers".
2. Again, why do not include return type in the signiture? I think you all have the experience that you can make a function call with checking the return or discarding the return. To have this flexibility, method and paramters of the method combination has to be unique.

Is this item helpful?  yes  no     Previous votes   Yes: 0  No: 0



Reply to this answer/comment  Help  
Re: Why the return type is not included in the method signature?
Varun CCS, Sep 19, 2006
This code is not permitted coz if I write

A a = new A();
a.getSomething();

the JVM wouldn't know which method to call !


Is this item helpful?  yes  no     Previous votes   Yes: 0  No: 0



Reply to this answer/comment  Help  
Re: Why the return type is not included in the method signature?
sreekanth pujari, Aug 1, 2007
Emil, For a moment lets assume that the return type is included in the method signature. Now look at the following snippet of code.
1  Class A
2  {
3  public int aMethod(int a, int b)
4     {
5          ....
6          return 0;
7     }
8    public String aMethod(int a, int b)
9     {
10          ....
11           return (0+"");
12      }
13      public static void main(String args[])
14      {
15           new A().aMethod(1,2);
16      }
17  }

What do you think the Compile should make out from line-15??
This is one of the reasons why the return type is not part of the method signature.

Is this item helpful?  yes  no     Previous votes   Yes: 0  No: 0



Reply to this answer/comment  Help  

Re: incomplete, thus incorrect
Eric Sun, Jun 5, 2006
This answer is wrong, they are important, but not signature. John is right.

Is this item helpful?  yes  no     Previous votes   Yes: 0  No: 0



Reply to this answer/comment  Help  
Re: incomplete, thus incorrect
Salil Agrawal, Aug 4, 2006  [replies:1]

The Java method signature contains only method name and parameter list. It doesn't contain visibility modifier, return type and throws clause because of the following reason..

Consider the following code:

class A {
  public A x(int l) {
    //some code
  }
}

class B extends A {
  public B x(int k) {
    //some code
  }
}

Now, implementation of method x in class B is an example of overriding, which is also logically correct. However, this would not have been the case if return type was made part of signature. In that case, JVM would not know the difference between the two methods since B instanceof A is true.



Is this item helpful?  yes  no     Previous votes   Yes: 0  No: 0



Reply to this answer/comment  Help  
Re[2]: incomplete, thus incorrect
Trivikram Reddy Mannem, Apr 20, 2007
when we will use the return type in case of overriding? what r all the previliges order?

Is this item helpful?  yes  no     Previous votes   Yes: 0  No: 0



Reply to this answer/comment  Help  
Re: incomplete, thus incorrect
Chris Carter, Apr 26, 2008
Deepak, you're thinking of the *declaration*. See http://java.sun.com/docs/books/tutorial/java/javaOO/methods.html.

Is this item helpful?  yes  no     Previous votes   Yes: 0  No: 0



Reply to this answer/comment  Help  


Ask A Question



 
Related Links

JavaLanguage FAQ

JavaLanguage Forum

Sun's Java Tutorial

What is the Java Platform

JavaWorld Magazine

IBM developerWorks

Java 2 SDK Tools and Utilities

The official Sun JavaDoc 1.1 documentation

The official Sun JavaDoc 1.2 documentation

The official Sun JavaDoc 1.3 documentation

The official Sun JavaDoc J2EE documentation

Wish List
Features
About jGuru
Contact Us

 



The Network for Technology Professionals

Search:

About Internet.com

Legal Notices, Licensing, Permissions, Privacy Policy.
Advertise | Newsletters | E-mail Offers