TOPIC 1.9 · STUDY

Method Signatures

A Java method signature is the method name plus the ordered parameter types. It does not include the return type, public or static, or the parameter names. A call runs the method whose signature matches the argument types in order; two headers with the same signature cannot both appear in one class.

1.9.A

Identifying a method signature

A method header announces how a method is named and what arguments it expects. From that header, the Java method signature is only the method name together with the ordered list of parameter types. Everything else on the line is useful documentation for a reader, but it is not part of the signature.

The return type is not in the signature. The modifiers public and static are not in the signature. The names you give the parameters are not in the signature. Only the name and the type sequence matter. That is why seal(int, boolean) is one signature, and seal(boolean, int) is a different one: the types are the same set, but the order changed.

public static int seal(int n, boolean ok)

That header returns an int, is public and static, and names its parameters n and ok. None of those details belong in the signature. The signature is seal(int, boolean).

A method header broken into boxes. The signature seal(int, boolean) sits in a green box. public static and the return type int sit in pale boxes because they are not part of the signature.

Green marks the name and ordered parameter types. Pale and gold mark pieces that appear on the header but are not in the signature.

Write the signature by keeping the method name and each parameter type in order, then drop the modifiers, the return type, and every parameter name. For the header above, that leaves seal(int, boolean).

Key points

  • A signature is the method name plus the ordered parameter types.
  • The return type is not part of the signature.
  • The modifiers public and static are not part of the signature.
  • Parameter names are not part of the signature.
  • Changing the order of the parameter types creates a different signature.

Reversing parameter types creates a different signature. seal(int, boolean) and seal(boolean, int) are not the same, even though both use one int and one boolean.

1.9.B

Matching a call to a signature

When you write a call, Java selects the method whose signature matches the argument types in order. Count the arguments, read each argument's type from left to right, and look for a header whose parameter types line up the same way. In this topic you read headers and match calls; you are not writing full class implementations.

public static double fare(int stops, boolean peak)

fare(3, true);

The call passes an int, then a boolean. That matches fare(int, boolean). A header fare(boolean, int) would not match, because the types are in the wrong order. A header fare(int) would not match, because the call supplies two arguments.

Two headers with the same signature cannot both live in one class. The return type does not save you: public static int tally(int n) and public static boolean tally(int n) both have signature tally(int), so they conflict. To offer more than one related method, the parameter type lists must differ.

public static void mist(int minutes)
public static void mist(int minutes, boolean fog)

mist(10);
mist(10, true);

Those two headers have different signatures: mist(int) and mist(int, boolean). The first call matches the one-parameter header. The second call matches the two-parameter header. Each call is chosen by the ordered argument types, not by the parameter names or by the word void.

Key points

  • A call selects the method whose signature matches the argument types in order.
  • Argument order matters as much as argument types.
  • Two headers with the same signature cannot both be in one class.
  • Return type and parameter names do not distinguish one signature from another.
  • In this topic, read headers and match calls rather than writing whole classes.

If two headers differ only in return type or only in parameter names, they still share one signature and cannot both appear in the same class. Change the ordered parameter types when you need a second method.

Practice this topic