---
title: "Method Identity and Signatures | FiveStudy"
description: "Name plus parameter list is identity; return type and parameter names are cosmetic."
url: https://fivestudy.com/guides/method-identity-signatures
---

[AP Computer Science A](https://fivestudy.com/ap-computer-science-a) · Study guide

# Method Identity and Signatures

[Figure: FiveStudy]

FiveStudy

AP® Computer Science A · Using Objects and Methods

AP® Computer Science A · Using Objects and Methods

# Method Identity and *Signatures*

What a method is, how parameters work, what makes up a method's signature, and how to use documentation and signatures to identify exactly which method a call will run.

1. What a Method Is 2. Procedural Abstraction 3. Parameters 4. Signatures 5. Choosing the Right Method 6. Signatures That Clash

Big Picture

A **method** is a named block of code that only runs when it is called. Methods let programmers package a task once and use it many times, and **procedural abstraction** allows a programmer to use a method by knowing what it does even without knowing how it was written. Methods receive information through **parameters**, variables declared in the method header. A method's **signature**, its name plus the ordered list of parameter types, identifies it; reading signatures and documentation is how you choose the correct method to call.

## 1 What a Method Is

A **method** is a named block of code that only runs when it is called. A **block** of code is any section of code enclosed in braces. Defining a method does not execute it; the method's statements run only when another part of the program calls it by name, and they run again every time it is called.

Methods solve a fundamental problem: repetition. If a program needs to compute a tax, print a formatted heading, or convert units in several places, writing that code once inside a method and calling it wherever needed keeps the program shorter, easier to read, and easier to fix. Change the method once, and every call benefits.

You have already been calling methods: `System.out.println`, `Math.sqrt`, and a `String`'s `length` are all methods someone else wrote. Every Java program also contains at least one method you write yourself: `main`, where execution starts.

| Term | Meaning | Example |
| --- | --- | --- |
| Method | A named block of code that only runs when it is called | `printReceipt()` |
| Block | Any section of code enclosed in braces | The body of a method |
| Parameter | A variable declared in the method header, usable inside the body | `int count` in `average(int total, int count)` |
| Signature | The method name and the ordered list of parameter types | `average(int, int)` |
| Procedural abstraction | Using a method by knowing what it does, not how it is written | Calling `Math.sqrt` without seeing its code |

Worked Example 1 Blocks and methods

### A Method Runs Only When Called

A method is a named block of code that only runs when it is called. Predict the output.

Code

```
public static void cheer()
{
    System.out.println("Go team!");
}

public static void warmUp()
{
    System.out.println("Stretch");
}
```

Output

```
Start
Go team!
Go team!
End
```

1. 1 **Defining is not running.** `warmUp` is defined but never called, so "Stretch" never prints.
2. 2 **Calling runs the block.** `cheer()` is called twice, so its body runs twice.
3. 3 **Order follows the calls.** The output order is the order of statements in `main`, with each call's output appearing where the call happens.

**Result:** The output is Start, Go team!, Go team!, End. A method's body runs only when, and each time, it is called.

## 2 Procedural Abstraction

**Procedural abstraction** allows a programmer to use a method by knowing what the method does, even if they do not know how it was written. When you call `Math.sqrt(81)`, you rely on the documented promise that it returns the square root; you never see its code. The method is a black box: arguments go in, a result comes out.

Abstraction is what makes large programs manageable. Each method hides its details behind a name and a signature, so a programmer working on one part of a program can use other parts without understanding them all at once. It also means a method's implementation can be improved later, made faster or more accurate, without changing any of the code that calls it, as long as what it does stays the same.

**Figure 1.** Procedural abstraction lets a programmer use a method by knowing what it does even if they do not know how it was written.

| To call a method, you need to know | Where to find it | You do not need to know |
| --- | --- | --- |
| Its name | The header or documentation | The statements in its body |
| The number, order, and types of its parameters | The signature | Its local variables |
| What it returns (or that it is void) | The return type | The algorithm it uses |
| What it does, and any requirements | The description, preconditions, postconditions | How it was tested or optimized |

Worked Example 2 Procedural abstraction

### Using a Method Without Reading Its Body

You are told only this: "`double distance(double x1, double y1, double x2, double y2)` returns the straight-line distance between two points." Use it to find the distance from (0, 0) to (3, 4) and from (1, 1) to (4, 5).

The method (you did not need to read this)

```
public static double distance(double x1, double y1, double x2, double y2)
{
    double dx = x2 - x1;
    double dy = y2 - y1;
    return Math.sqrt(dx * dx + dy * dy);
}
```

Output

```
5.0
5.0
```

1. 1 **Use the signature.** Four `double` parameters, in the order x1, y1, x2, y2. The `int` arguments widen to `double` automatically.
2. 2 **Use the description.** The method returns a distance, a `double`, so the result can be printed or stored.
3. 3 **Results.** Both distances are 5.0, which matches the 3-4-5 right triangle.

**Result:** Both calls print `5.0`. Procedural abstraction means the documentation was enough; the method body could change without affecting how you call it.

## 3 Parameters: How Methods Receive Information

A **parameter** is a variable declared in the header of a method or constructor, and it can be used inside the body of the method. Parameters allow values, called **arguments**, to be passed to the method when it is called. In `public static double average(int total, int count)`, the parameters are `total` and `count`; a call such as `average(349, 4)` passes the arguments 349 and 4.

Each parameter has a type and a name, just like any variable declaration. The type determines what kinds of arguments can be passed; the name is how the method body refers to the value. Parameter names matter only inside the method: a caller never uses them, and two methods may use the same parameter names without any conflict.

Some methods need no information at all. Their parameter list is empty, written as a pair of parentheses with nothing inside, such as `printLine()`. The parentheses are still required in both the header and every call.

**Figure 2.** A method header states the return type, name, and parameters. The signature is only the method name and the ordered list of parameter types.

## 4 Method Signatures

A **method signature** for a method with parameters consists of the method name and the ordered list of parameter types. A method signature for a method without parameters consists of the method name and an empty parameter list. The signature of `public static double average(int total, int count)` is `average(int, int)`.

Several parts of a header are deliberately left out of the signature. The **return type** is not included, because a call does not specify what type it expects back. **Parameter names** are not included, because a call passes only values. **Modifiers** such as `public` and `static` are not included. What remains, the name and the number, types, and order of the parameters, is exactly the information a call provides, which is why it identifies the method.

| Part of the header | In the signature? | Example |
| --- | --- | --- |
| Method name | Yes | `area` |
| Number of parameters | Yes | `area(int)` vs `area(int, int)` |
| Types of parameters, in order | Yes | `f(int, double)` vs `f(double, int)` |
| Parameter names | No | `move(int x)` and `move(int steps)` are the same |
| Return type | No | `int area(int)` and `double area(int)` clash |
| Modifiers such as public static | No | They control access and how the method is called |

Worked Example 3 Reading headers

### Finding the Signature in a Header

Give the signature for each method header.

1. `public static void printLine()`
2. `public static int max(int a, int b)`
3. `public static double pay(double hours, int rate)`
4. `public static String repeat(String word, int times)`
1. 1 **1.** `printLine()`: no parameters, so the parameter list is empty.
2. 2 **2.** `max(int, int)`.
3. 3 **3.** `pay(double, int)`: the order matters; `pay(int, double)` would be a different signature.
4. 4 **4.** `repeat(String, int)`. The return type `String` is not part of the signature.

**Result:** The signatures are `printLine()`, `max(int, int)`, `pay(double, int)`, and `repeat(String, int)`: the name plus the ordered parameter types.

Common Mistake 1 · Including the Return Type or Parameter Names

The signature of `public static int max(int a, int b)` is `max(int, int)`, not `int max(int a, int b)`. Leave out the return type, the parameter names, and the modifiers; keep the name and the ordered parameter types.

## 5 Choosing the Right Method to Call

A class may contain several methods with the same name but different signatures, a situation called **overloading**. When a call is made, Java identifies the correct method by matching the arguments to a signature: the same number of arguments, in the same order, with types compatible with the parameter types. If exactly one method fits, it runs; if none fits, the call does not compile.

When reading documentation to decide which method to use, follow the same process. Identify what values you have and their types, then find the signature whose parameter list matches them, and read its description to confirm it does what you need. Type compatibility includes automatic widening: an `int` argument can be passed to a `double` parameter if no better match exists.

**Figure 3.** To identify which method a call uses, compare the arguments with each candidate signature: same number of values, in the same order, with compatible types.

Worked Example 4 Choosing a method

### Which Version Runs?

A class has three methods named `label`. Predict the output.

Code

```
public static String label(int n)
{
    return "int " + n;
}

public static String label(double d)
{
    return "double " + d;
}

public static String label(String s, int n)
{
    return s + " x" + n;
}
```

Output

```
int 7
double 7.0
pen x3
```

1. 1 **`label(7)`.** One `int` argument exactly matches `label(int)`.
2. 2 **`label(7.0)`.** One `double` argument matches `label(double)`.
3. 3 **`label("pen", 3)`.** A `String` then an `int` matches `label(String, int)`.

**Result:** Each call runs the method whose signature matches its arguments, printing `int 7`, `double 7.0`, and `pen x3`.

Common Mistake 2 · Ignoring Parameter Order

`fee(int, double)` and `fee(double, int)` are different methods. Passing arguments in the wrong order either calls the wrong method or does not compile. When you read a signature, read the types left to right and match your arguments in exactly that order.

## 6 Signatures That Clash

Because a call is matched by signature, no two methods in the same class may share a signature. Two methods that differ only in return type, or only in parameter names, have identical signatures, and the compiler reports that the method is already defined. To create a legitimate second version of a method, change its name or its parameter list.

Signatures therefore act as a method's identity. When designing methods, choose names that describe what they do and parameter lists that describe what they need. Overloads should all perform the same conceptual task, just with different inputs, so that a reader seeing the name knows what to expect regardless of which version is called.

**Figure 4.** Two methods in the same class must have different signatures. Changing the number, types, or order of parameters creates a new signature; changing only the return type or parameter names does not.

Worked Example 5 Illegal duplicates

### Two Methods That Differ Only in Return Type

Why does this class not compile?

Code that does not compile

```
public static int half(int n)
{
    return n / 2;
}

public static double half(int n)
{
    return n / 2.0;
}
```

1. 1 **Same signature.** Both methods are `half(int)`. The return type is not part of the signature, so Java sees two definitions of the same method.
2. 2 **Why Java forbids it.** In a call such as `half(5)`, nothing tells Java which version to run.
3. 3 **Fix.** Give one method a different name, such as `halfExact`, or a different parameter list.

**Result:** The compiler reports that `half(int)` is already defined. Two methods in a class must differ in their signatures, not just their return types.

Worked Example 6 Parameter names

### Renaming Parameters Does Not Create a New Method

Does this compile?

Code that does not compile

```
public static void move(int x)
{
    System.out.println("x: " + x);
}

public static void move(int steps)
{
    System.out.println("steps: " + steps);
}
```

1. 1 **Signatures.** Both are `move(int)`; parameter names are not part of the signature.
2. 2 **Takeaway.** A call like `move(3)` provides only a value, never a parameter name, so names cannot distinguish methods.

**Result:** It does not compile: `move(int)` is defined twice. Only the number, types, and order of parameters distinguish signatures.

Common Mistake 3 · Overloading by Return Type

Writing `int area(int s)` and `double area(int s)` in the same class does not create two methods; it is a compile-time error. A call like `area(5)` would give Java no way to choose. Overloads must differ in their parameter lists.

Common Mistake 4 · Thinking a Defined Method Runs Automatically

Writing a method does not execute it. Only `main` runs automatically when the program starts; every other method runs only when it is called. When tracing, follow the calls, not the order in which methods appear in the file.

### Four Habits for Working With Methods

#### 1. Defining Is Not Running

A method's body runs only when, and each time, it is called.

#### 2. Signature = Name + Types

Ordered parameter types; no return type, no names.

#### 3. Match Number, Order, Type

That is how Java chooses among overloaded methods.

#### 4. Trust the Description

Procedural abstraction: know what a method does, not how.

Core Synthesis

A method is a named block of code that only runs when it is called; a block of code is any section of code enclosed in braces. Procedural abstraction allows a programmer to use a method by knowing what the method does even if they do not know how it was written. A parameter is a variable declared in the header of a method or constructor and can be used inside the body of the method, allowing values or arguments to be passed and used. A method signature consists of the method name and the ordered list of parameter types, or the name and an empty parameter list for a method without parameters, and it identifies which method a call will run.

FiveStudy AP® Computer Science A Study Guide · Using Objects and Methods · Method Identity and Signatures Back to top ↑
