π1.9: Method Signatures
Table of Contents
π This page is a condensed version of CSAwesome Topic 1.9
Methods and Procedural Abstraction
Up until now, all of our code has been written as line-by-line statements in the main method, executed like step-by-step instructions, but complex programs are made up of many methods. We divide a program into methods to organize the code and avoid repetition.
- Method
- A named block of code that performs a task/process when it is called.
A block of code is any section enclosed in { }. These named blocks of code go by many names in different programming languages: functions, procedures, subroutines, etc. In Java they are called methods, as in a method of doing something.
In this unit, you will learn how to use methods written by other programmers. In latter units, you will learn how to write your own methods.
- Procedural Abstraction
- Allowing a programmer to use a method without knowing exactly how it works, i.e. "abstracting away" the details.
Procedural abstraction means we can use a method that someone else wrote without knowing exactly how it works. We just need to know the methodβs name to call it and what it needs to do its job.
For example, we donβt need to know how a car exactly works in order to drive. π We know that if we hit the brakes, the car will stop; we can still use the brakes even if we donβt really know how they exactly work.
Method Calls
π£ A method call is when the code βcalls outβ a methodβs name to activate/run it. It always includes parentheses () and may include data inside.
methodName();
For example, when we write the statement
System.out.println("Hello World");, we are calling theprintln()method to print out the text βHello Worldβ.
Every method call is followed by parentheses. The parentheses () after method names are there in case you need to give the method some parameters/data to do its job, which we will see in the next lesson.
Example: Old MacDonald Song
This program in the shows how a song can be divided up into methods. Click on the next button below the code to βstep throughβ the code line-by-line, and watch the red arrow jump to the method that is being run.
π Execution of Java code always begins in the
mainmethod in the current class. Then, the flow of control skips from method to method as they are called. Notice that when a method ends, it returns to the line right after the method call.
Go into edit mode in the Visualizer and scroll down to the main() method. Add lines of code to the main method to print the second verse of the Old MacDonald Song:
- Call the method
intro() - Print out the line βAnd on that farmβ¦β with a duck or another animal
- Call the method
chorus() - Print out the lines with the appropriate animal sounds
- Call the method
intro()again
β A method call interrupts the sequential execution of statements, causing the program to first execute the statements in the method before continuing. Once the last statement in the method has been executed or a return statement is executed, the flow of control is returned to the point immediately following where the method was called.
Method Signature/Header
When using methods in a library or API, we can look up the method signature/header) in its documentation.
A method header is the first line of a method that defines:
- Method name
- Return type (e.g.,
voidmeans no return value) - Parameter list (variables and data types in parentheses; may be empty, but parentheses are required)
Example: PrintStream println Methods
From Oracle - Java Documentation:
println()β no parameters, just prints a new lineprintln(String x)β prints aStringprintln(int x)β prints anint
We can call these methods with the appropriate arguments to print out the value we want. The argument is the actual value that is passed to the method when it is called. Here are the method calls that correspond to the method signatures above:
System.out.println();System.out.println("Hello World");System.out.println(42);

Parameters vs. Arguments
We can make methods even more powerful and more abstract by giving them parameters for data that they need to do their job. This allows values, or arguments, to be passed and used by a method.
- A parameter (βformal parameterβ) is a placeholder variable declared in the method header.
- An argument (βactual parameterβ) is the value passed into the method when called.
- The argument must be compatible with the parameterβs data type.
Example:
- Signature:
println(String x)β parameter isString x - Call:
println("Hello World");β argument is"Hello World"
Many people use the terms interchangeably, but technically the parameter is the placeholder and the argument is the value.
EXAMPLE: Old MacDonald Improved
Letβs take another look at the Old MacDonald Song and see if we can replace more repeated code with methods with parameters.
Notice that each verse of the song is similar, except it is about a different animal and the sound it makes. We can make a method called verse that takes the animal and its sound to print out any verse! The parameter variables animal and sound will hold different values when the method is called.
Step through this new version of the Old MacDonald program in the and watch how the argument are saved in the parameter variables with each call to the verse method.
Go into edit mode in the Visualizer and scroll down to the main() method. Add on to the song by calling methods as needed:
- Print another verse with the animal
"goose"and the sound"honk"by calling theverse()method with the appropriate arguments. - Then call
intro()again. - Repeat with another animal and sound of your choice.
Note that the main method can now just consist of calls to the intro() and verse() methods. Main methods often look like an outline for the program, calling methods to do the work.
When a method is called, the correct method definition (set of code instructions) is found by checking the method signature or header at the top of the method definition to match the method name, the number of arguments, the data types for the arguments and the return type.
// Method signature (parameters)
public static void verse(String animal, String sound) { }
// Method call (arguments)
verse("cow", "moo");

Hereβs what that looks like with the two method calls above. The arguments like βcowβ and βmooβ are saved into the parameter variables
animalandsoundwith each method call.
Java uses call by value when it passes arguments to methods. This means that a copy of the value in the argument is saved in the parameter variable. Call by value initializes the parameters with copies of the arguments. If the parameter variable changes its value inside the method, the original value outside the method is not changed.
Overloading Methods
Methods are overloaded when they share the same name but have different parameter lists. The compiler determines which method to call based on the number and types of arguments passed to the method.
Example:
println()β prints newlineprintln(String x)β prints a stringprintln(int x)β prints an integer
Writing Methods
Up until this unit, you wrote all your code in the main method, but now we are using lots of methods. Why have multiple methods instead of just one?
Procedural Abstraction allows us to name a block of code as a method and call it whenever we need it, abstracting away the details of how it works.
This serves to organize our code by function and reduce repetition of code. In addition, it helps with debugging and maintenance since changes to that block of code only need to happen in one place!
Here are some of the main reasons to use multiple methods in your programs:
- Organization and Reducing Complexity: organize your program into small sections of code by function to reduce its complexity. Divide a problem into subproblems to solve it a piece at a time.
- Reusing Code: avoid repetition of code. Reuse code by putting it in a method and calling it whenever needed.
- Maintainability and Debugging: smaller methods are easier to debug and understand than searching through a large main method.
How to Write a Method
There are three steps to creating and calling a custom method:
- Method Definition: Write the methodβs header/signature and body code inside the class that defines the object.
// Step 1: Define the method in the class // Method HEADER/SIGNATURE public void methodName() { // Method BODY for the repeatable code } - Object Instance: Declare an object of your class type in the main method or from outside the class.
// Step 2: Declare an object ClassName objectName = new ClassName(); - Method Call: Whenever you want to use the method, call it on the object by using the dot
.operator// Step 3: Call the object's method objectName.methodName();- π₯ If the method requires any INPUT to work, provide those values in the parenthesis. These are known as arguments, or actual parameters.
- π€ If the method provides any OUTPUT, meaning it has a return value, store it in a variable of the appropriate type (see below for more details on using methods that return values).
πΆ Letβs look at an example with lots of repetition of code and create methods to reduce the repetition of code. You can sing along here: This Old Man Song.
You may have noticed that the chorus is repeated βWith a knick knack paddy whack, give a dog a bone. This old man came rolling home.β When you see repeated code, that is a signal for you to make a new method!
For example, here is a chorus() method definition that we could write for the βThis Old Man Songβ:
public void chorus() {
System.out.println("With a knick knack paddy whack, give a dog a bone.");
System.out.println("This old man came rolling home.");
}
Run the tester code in the Java visualizer to see the song This Old Man print out. Can you replace the last two lines in the second verse in the main method with a call the
chorus()method instead?
Method Input: Formal Parameters
You may have noticed even more repetition in the song above. What about the lines of each verse? Notice that every word is repeated except the last ones that include a number and a rhyme such as one/thumb and two/shoe.
System.out.println("This old man, he played one.");
System.out.println("He played knick knack on my thumb.");
...
System.out.println("This old man, he played two.");
System.out.println("He played knick knack on my shoe.");
We can make methods even more powerful and more abstract by giving them parameters for data that they need to do their job - think of it like taking INPUT before the process. We can make a method called verse that takes the number and the rhyme to print out any verse!
public void verse(String number, String rhyme) {
System.out.println("This old man, he played " + number);
System.out.println("He played knick knack on my " + rhyme);
}
Run the tester code in the Java visualizer to see the song This Old Man print out using the
verseandchorusmethods. Can you add verse three with the rhyme βkneeβ? Can you add verse four with the rhyme βdoorβ? How many verses do you know?
π€΅ββοΈπ€΅π€΅ββοΈ When you define your own method, the variables you specify for it in the method header are called formal parameters.
π£ When you call the method to do its job, you give or pass in arguments or actual parameters to it that are then saved in these local parameter variables.
When a method is called, the right method definition is found by checking the method signature or header at the top of the method definition to match the following:
- The method name
- The number of arguments
- The data types for the arguments
- The
returntype (eithervoidor a data type likeint,String, etc.)
For example, imagine we are calling the methods for the This Old Man song:
Song mySong = new Song();
mySong.verse("one", "thumb");
mySong.chorus();
mySong.verse("two", "shoe");
mySong.chorus();
Hereβs what that looks like with the 2 method calls above. Notice how the parameter variables get new values with every method call.

Java uses Call by Value when it passes arguments to methods. This means that a copy of the value in the argument is saved in the parameter variable. If the parameter variable changes its value inside the method, the original value outside the method is not changed.
If you pass in an argument that holds a reference to an object, like a String or Person or Turtle object, a copy of this reference is passed in and saved in the parameter variable. The formal parameter and the actual parameter (argument) are then aliases, both refering to the same object.
Java was designed this way to avoid copying large objects from method to method. Remember when we discussed reference aliases with
Turtleobjects who are set equal to one another.

Method Output: Return Values
π€ Methods can also return values of any type back to the calling method, which can be considered OUTPUT. Recall that non-void methods, like we saw when writing getter/accessor methods, return a value of a specific data type.
If a certain method returns a value, the calling method should then do something with this return value, like printing it out or assigning it to a variable (of the same type).
// Printing a value returned from a method call
System.out.print("The student's name is " + student.getName() );
// Storing a return value in a new variable
String studentName = student.getName();
Summary
-
(AP 1.9.A.1) A method is a named block of code that only runs when it is called. A block of code is any section of code that is enclosed in braces.
-
(AP 1.9.A.1) Procedural abstraction allows a programmer to use a method by knowing what the method does even if they do not know how the method was written.
-
(AP 1.9.B.5) A method call interrupts the sequential execution of statements, causing the program to first execute the statements in the method before continuing. Once the last statement in the method has been executed or a return statement is executed, the flow of control is returned to the point immediately following where the method was called.
-
(AP 1.9.A.2) A parameter is a variable declared in the header of a method or constructor and can be used inside the body of the method. This allows values or arguments to be passed and used by a method or constructor.
-
(AP 1.9.A.2) 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.
-
(AP 1.9.B.3) An argument is a value that is passed into a method when the method is called. The arguments passed to a method must be compatible in number and order with the types identified in the parameter list of the method signature. When calling methods, arguments are passed using call by value.
-
(AP 1.9.B.3) Call by value initializes the parameters with copies of the arguments.
-
(AP 1.9.B.4) Methods are said to be overloaded when there are multiple methods with the same name but different signatures.
Acknowledgement
Content on this page is adapted from Runestone Academy - Barb Ericson, Beryl Hoffman, Peter Seibel.