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.
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.
1What 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 |
A Method Runs Only When Called
A method is a named block of code that only runs when it is called. Predict the output.
public static void cheer()
{
System.out.println("Go team!");
}
public static void warmUp()
{
System.out.println("Stretch");
}
Start
Go team!
Go team!
End
- 1Defining is not running.
warmUpis defined but never called, so "Stretch" never prints. - 2Calling runs the block.
cheer()is called twice, so its body runs twice. - 3Order follows the calls. The output order is the order of statements in
main, with each call's output appearing where the call happens.
2Procedural 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.
| 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 |
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).
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);
}
5.0
5.0
- 1Use the signature. Four
doubleparameters, in the order x1, y1, x2, y2. Theintarguments widen todoubleautomatically. - 2Use the description. The method returns a distance, a
double, so the result can be printed or stored. - 3Results. Both distances are 5.0, which matches the 3-4-5 right triangle.
5.0. Procedural abstraction means the documentation was enough; the method body could change without affecting how you call it.3Parameters: 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.
4Method 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 |
Finding the Signature in a Header
Give the signature for each method header.
public static void printLine()public static int max(int a, int b)public static double pay(double hours, int rate)public static String repeat(String word, int times)
- 11.
printLine(): no parameters, so the parameter list is empty. - 22.
max(int, int). - 33.
pay(double, int): the order matters;pay(int, double)would be a different signature. - 44.
repeat(String, int). The return typeStringis not part of the signature.
printLine(), max(int, int), pay(double, int), and repeat(String, int): the name plus the ordered parameter types.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.
5Choosing 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.
Which Version Runs?
A class has three methods named label. Predict the output.
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;
}
int 7
double 7.0
pen x3
- 1
label(7). Oneintargument exactly matcheslabel(int). - 2
label(7.0). Onedoubleargument matcheslabel(double). - 3
label("pen", 3). AStringthen anintmatcheslabel(String, int).
int 7, double 7.0, and pen x3.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.
6Signatures 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.
Two Methods That Differ Only in Return Type
Why does this class not compile?
public static int half(int n)
{
return n / 2;
}
public static double half(int n)
{
return n / 2.0;
}
- 1Same signature. Both methods are
half(int). The return type is not part of the signature, so Java sees two definitions of the same method. - 2Why Java forbids it. In a call such as
half(5), nothing tells Java which version to run. - 3Fix. Give one method a different name, such as
halfExact, or a different parameter list.
half(int) is already defined. Two methods in a class must differ in their signatures, not just their return types.Renaming Parameters Does Not Create a New Method
Does this compile?
public static void move(int x)
{
System.out.println("x: " + x);
}
public static void move(int steps)
{
System.out.println("steps: " + steps);
}
- 1Signatures. Both are
move(int); parameter names are not part of the signature. - 2Takeaway. A call like
move(3)provides only a value, never a parameter name, so names cannot distinguish methods.
move(int) is defined twice. Only the number, types, and order of parameters distinguish signatures.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.
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.
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.