Method Signatures
A method is a block of code with a name. This lesson reads method headers and calls, using a class that plans a music student's practice.
public class Practice
{
public static void warmUp()
{
System.out.print("scales ");
}
public static int minutes(int pieces, int each)
{
return pieces * each;
}
public static void main(String[] args)
{
int pieces = 3;
System.out.print("Plan: ");
warmUp();
int planned = minutes(pieces, 12);
System.out.println("then " + planned);
}
}
Methods and calls
A block of code is any section enclosed in braces. The class body is a block, and each method's body is a block inside it.
The method
warmUp is written above main, but writing it doesn't run it. It runs only when main calls it, with the statement warmUp();. When that call is reached, main stops and the statements in warmUp run. Then control returns to main at the statement right after the call, and in the end the program prints Plan: scales then 36.A programmer can call
minutes knowing only that it gives the total practice time, without reading its body. Calling a method from a description of its job, without its code, is procedural abstraction.Parameters and signatures
The header
public static int minutes(int pieces, int each) declares two parameters, pieces and each. They're variables the body can use, and each call gives them values.A method's signature is its name and its parameter types, in order. The signature of
minutes is minutes(int, int). The return type int isn't part of it, and neither are the names pieces and each. A method with no parameters has an empty list, so the signature of warmUp is warmUp().Return values
The return type in a header names the type of value the method gives back, and
void in that place means it gives back nothing. Since warmUp is void, it's called as a statement by itself, and int x = warmUp(); doesn't compile, because there's no value to store.The method
minutes returns an int. In main, its value 36 is stored in planned. It could also be used in an expression, as in minutes(3, 12) + 15. Calling it as a statement by itself, minutes(3, 12);, compiles, but the 36 is lost.Overloading
A class can have two methods with the same name if their signatures differ, and then the methods are overloaded.
public static void log(int minutes)
public static void log(String piece, int minutes)
The signatures are
log(int) and log(String, int). A call picks between them by its arguments. log(20) runs the first one, and log("Etude", 20) runs the second. The call log(20, "Etude") doesn't compile, because no log method takes an int first and a String second.Arguments are copied
The values in a call's parentheses are its arguments. When the call runs, each parameter is created, and it starts with a copy of its argument.
public static int plan(int practiced)
{
practiced = practiced * 2;
return practiced - 5;
}
Suppose
main declares int practiced = 25; and then runs int next = plan(practiced);. The parameter starts as a copy, 25, becomes 50, and the method returns 45, which goes into next. The variable practiced in main still holds 25 after the call. The parameter has the same name, but it's a separate variable, and the method changed only that copy.