From the free course AP Computer Science A on Lykke

1.9 Method Signatures

Key concepts: Class design using attributes and behaviors · Method signatures and corresponding method descriptions · File object construction with a pathname · Two-dimensional array indexing by row and column · Writing methods that process a specified row · Summing point values across a row · Constructors and object initialization · Accessor and mutator methods such as getSignature and addSignature · Using method calls to test expected behavior · Handling distinct cases in method implementations

A method’s signature is its identity at the call boundary: the method name together with its parameter types. Before writing a class, design its attributes—the data it stores—and behaviors—the operations it performs—using natural language, a sketch, or a class diagram.

1.9 Method Signatures

A method’s signature is its identity at the call boundary: the method name together with its parameter types. Before writing a class, design its attributes—the data it stores—and behaviors—the operations it performs—using natural language, a sketch, or a class diagram.

CED Essential Knowledge 3.1.A.7: Prior to implementing a class, design each class’s attributes and behaviors using natural language or diagrams.

A useful design separates what an object knows from what an object does:

SignedText
--------------------------------
signature : String

getSignature() : String
addSignature(firstName, lastName) : void

The diagram is not decoration. It predicts the code: signature is an attribute, while getSignature and addSignature are behaviors. This is AP Skill 1 — Design Code, because the programmer decides the class structure and method responsibilities before implementation.

Signature versus full method declaration

In Java, do not confuse a method’s signature with its complete declaration.

Part Example Role
Method signature addSignature(String, String) Method name plus parameter types; identifies the method for calls and overloading
Access modifier public Controls where the method can be accessed
Return type void States what value, if any, the method produces
Parameter names firstName, lastName Local names used inside the method body
Full declaration public void addSignature(String firstName, String lastName) Complete instruction for declaring the method

Thus, public String getSignature() has the signature getSignature(), not String getSignature(). The return type and access modifier belong to the full declaration; they are not part of the Java method signature.

A method-reference table uses the same idea in a compact form: signatures appear in one column, and their intended behavior appears beside them. The description is a contract—what the method must do, not how it must do it.

Worked example: SignedText

Suppose a SignedText object stores a signature assembled from a first name and last name. Its interface can be specified as follows:

Method signature Description
SignedText(String, String) Constructs a SignedText object using a first name and last name
getSignature() Returns the stored signature
addSignature(String, String) Updates the stored signature using the supplied names

A client can construct and use the object like this:

SignedText note = new SignedText("henri", "dubois");

System.out.println(note.getSignature());
note.addSignature("marie", "curie");
System.out.println(note.getSignature());

One reasonable implementation is:

public class SignedText {
    private String signature;

    public SignedText(String firstName, String lastName) {
        signature = firstName + " " + lastName;
    }

    public String getSignature() {
        return signature;
    }

    public void addSignature(String firstName, String lastName) {
        signature = firstName + " " + lastName;
    }
}

The trace is:

new SignedText("henri", "dubois")
signature = "henri dubois"

getSignature()
returns "henri dubois"

addSignature("marie", "curie")
signature becomes "marie curie"

getSignature()
returns "marie curie"

getSignature retrieves the current attribute value. addSignature changes that value. The method names must be called and implemented according to their intended functionality: a getter should return the stored signature, while the update method must assign the new signature rather than merely print it. This demonstrates AP Skill 2 — Develop Code and AP Skill 3 — Analyze Code through implementation and state tracing.

Reading signatures in Java APIs

Library references apply the same contract pattern. The File constructor has the signature File(String pathname): it creates a File object from a String representing a pathname.

File inputFile = new File("scores.txt");

A Scanner can then be constructed from that File:

Scanner input = new Scanner(inputFile);
int score = input.nextInt();

The constructor signature tells you exactly what argument type is required. Passing a File to Scanner(File f) is different from passing a String pathname directly; the compiler uses the declared parameter types to determine whether the call is valid.

Signatures and two-dimensional data

Method specifications become especially important when processing a two-dimensional array. For an array named board, the first index selects the row, and the second selects the column:

board[row][column]

Consider a GameBoard method with the intended behavior:

public int getPointsForRow(int targetRow)

Its signature is getPointsForRow(int). The method must add the point values of every space in the specified row, even when the spaces have different values.

public int getPointsForRow(int targetRow) {
    int total = 0;

    for (int col = 0; col < board[targetRow].length; col++) {
        total += board[targetRow][col].getPoints();
    }

    return total;
}

The call getPointsForRow(0) returns $1300$ for the provided board, while getPointsForRow(2) returns $2000$. The crucial indexing rule is board[targetRow][col]: changing the first index changes the row being summed; changing the second index moves across that row.

Misconception check

Misconception: “The return type is part of the method signature.” In Java, getPointsForRow(int) identifies the method, while int describes the value returned by the full declaration. Also, board[col][row] does not mean the same thing as board[row][col]; reversing the indices can sum an entirely different set of spaces.

Retrieval check: What is the signature of public double average(int count)? What does board[2][4] select? Answers: average(int); row $2$, column $4$.

1.9 Method Signatures - AP Computer Science A - image 1
1.9 Method Signatures - AP Computer Science A - image 1
1.9 Method Signatures - AP Computer Science A - image 2
1.9 Method Signatures - AP Computer Science A - image 2
1.9 Method Signatures - AP Computer Science A - diagram 1
1.9 Method Signatures - AP Computer Science A - diagram 1

Source Materials

The whole course on Lykke · Browse all public course wikis