go to previous page   go to home page   go to next page

Answer:

The signature of a method is the name of the method and the types in its parameter list.


Signature

A signature looks like this:

someMethod( int, double, String )

The return type is not part of the signature. The names of the parameters do not matter, just their types.

The name of a method is not enough to uniquely identify it. There may be several versions of a method, each with different parameters. The types in the parameter are needed to specify which method you want.

The return type is not part of the signature because of the following situation: Say that there are two methods in the same class. One is defined like this:

int someMethod( int x, double y, String z )
{
  //method body
}

Another method is defined like this:

short someMethod( int a, double b, String c )
{
  // method body
}

Somewhere in the program someMethod() is called:

long w = item.someMethod( 12, 34.9, "Which one?" );

You can not tell which someMethod() is called for. The return value on the right of the = could be either int or short. Both can be converted to a long value needed for the variable w. In other words, the signatures of the two methods are not unique, and the compiler would complain.

It would be OK if there were a second method defined like this:

short someMethod( double a, double b, String c )
{
  // method body
}

The signature of this method differs from the signature of the first because the types of the parameters are different.


QUESTION 2:

Do these two methods have different signatures?

double aMethod( int x, String y){. . .}

short  aMethod( int a, double b){. . .}