Opening the reading…
Opening the reading…
PROGRAMMING FUNDAMENTALS › JAVA FUNDAMENTALS
A method declaration tells Java the method's name, return type, and body. It creates an action that can be used later, but Java does not run the body merely because it reaches the declaration in the class. In this example, printValues() is declared once and its body contains the statements that print the array values.
public class Main {
public static void main(String[] args) {
printValues();
printValues();
}
static void printValues() {
int[] values = {4, 2, 7, 1};
for (int value : values) {
System.out.println(value);
}
}
}The line static void printValues() is the method header. It gives the method a name and says that it does not produce a value for its caller. The statements between its braces are the method body. The line printValues(); in main is different: it is a call, and a call tells Java to start executing the body.
In the complete class above, which pair correctly identifies the declaration and the line that causes printValues() to run?
Checkpoints are not graded. They are here so you catch yourself before the quiz does — stuck, ask the tutor on the right.
When main reaches its first printValues(); statement, execution leaves that point in main and enters the printValues() body. The loop then visits the values 4, 2, 7, and 1, printing each one. After the body finishes, execution returns to main and continues with the statement after the call.
main begins
|
v
first printValues();
|
v
printValues() body
|
+-- print 4
+-- print 2
+-- print 7
+-- print 1
|
v
return to main
|
v
second printValues();The call does not create a second main method or copy the body into main. It temporarily changes where Java is executing. The call waits while the loop completes, then the caller continues. Because printValues() is void, this example only needs the transfer into the body and the return to the next statement, not a value passed back to main.
The parentheses after printValues are part of the method call syntax. Since this method has no parameters, the parentheses are empty, but they are still required. The semicolon ends the statement, so the complete call is printValues();.
printValues(); // valid call
printValues; // invalid: a method name is not a statement
static void printValues(); // invalid inside mainWriting printValues; does not tell Java to invoke a method, because it has no call parentheses. Writing static void printValues(); inside main is also not a call. It attempts to write a method declaration inside another method, where Java does not allow method declarations, and it also provides no method body.
Repair this line in main so it invokes the existing method.
printValues;Checkpoints are not graded. They are here so you catch yourself before the quiz does — stuck, ask the tutor on the right.
The declaration for printValues() appears only once, but main contains two calls. The first call runs the complete body and prints 4, 2, 7, and 1. After execution returns to main, the second call enters that same body again and prints the same four values in the same order.
First call: printValues();
prints 4 2 7 1
Second call: printValues();
prints 4 2 7 1The method declaration is a reusable definition, not an automatic instruction. A call is the event that starts one execution. Therefore, changing the number of calls changes how many times the body runs, while keeping the declaration unchanged.