WEWwwbvxnvbxmnhsgfkdjfcn
Methods
CPCS 202
WEEK 9
LECTURE 3
Method Call Stack
A method is invoked (also referred to as called), which causes flow of control to
jump to the method that is being invoked.
Flow of control then executes the Flow of control then executes the
statements within the method. Of course, the method being executed might invoke another method, causing flow of
control to jump to this other method.
All method calls are maintained in a structure known as the call stack. The
current method that is executing is at the top of the call stack. When this method top of the call stack. When this method completes executing, it is removed
from the top of the call stack, and the flow of control returns to the previous
method on the stack.
When a new method is invoked, this new method gets placed at the top of the call stack. The first method that is invoked in your Java program is main(), which is
your Java program is main(), which is
invoked by the JVM. Therefore, main() is at the bottom of your method call
stack.
Suppose that main() invokes a method called turnOn(), and the turnOn()
method invokes a setVolume() method, which in turn invokes the println()
which in turn invokes the println()
method. Because println() is at the top of the call stack, the flow of control is
currently within println().
The setVolume() method is waiting for
println() to finish, the turnOn() method is waiting for setVolume() to finish, and so on down the call stack.
down the call stack.
println() setVolume() turnOn() main()
Invoking Methods
A method is invoked, causing it to be
placed at the top of the call stack until it is finished executing. When a method is done executing, three things can occur:
done executing, three things can occur:
■■ The method returns a value, in which case a primitive data type or reference is passed back to the caller of the
method.
■■ The method does not return a value, in which case the return value is declared
as void.
■■ The method throws an exception,
■■ The method throws an exception, which is thrown back to the caller of the method.
Example : Date class
public class Date {
public int day, month, year;
public int getDay() {
System.out.println(“Inside getDay method”);
return day;
return day;
}
public void printDate() {
System.out.println(“Inside printDate method”);
System.out.println(month + “/” + day + “/” + year);
} }
The Date class has three fields, all of type int: day, month, and year. The Date
class also has two methods: getDay() and printDate(). The getDay() method
printDate(). The getDay() method
declares that it returns an int, and notice within getDay() it returns the day
field, which is an int.
The printDate() method is declared void
and does not return a value. Both methods have an empty parameter list, which is
denoted by the empty parentheses.
denoted by the empty parentheses.
DateProgram
public class DateProgram {
public static void main(String [] args) {
System.out.println(“Within main...”);
Date today = new Date();
today.day = 25;
today.month = 12;
today.month = 12;
today.year = 2003;
System.out.println(“The day is “ + today.getDay());
System.out.println(“Printing the date...”);
today.printDate();
System.out.println(“What is displayed next?”);
today.getDay();
}
Output of the DateProgram.
Notice that the string “The day is “ is not
displayed yet. The call to System.out.println()
has not occurred yet because order of operations requires that the method call to getDay()
requires that the method call to getDay()
occur before the call to println().After getDay() returns a value, the string concatenation is
evaluated, and then println() will be invoked.