• Tidak ada hasil yang ditemukan

Methods CPCS 202

N/A
N/A
Protected

Academic year: 2025

Membagikan "Methods CPCS 202"

Copied!
15
0
0

Teks penuh

(1)

WEWwwbvxnvbxmnhsgfkdjfcn

Methods

CPCS 202

(2)

WEEK 9

LECTURE 3

(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.

(4)

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.

(5)

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.

(6)

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().

(7)

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()

(8)

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.

(9)

■■ 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.

(10)

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);

} }

(11)

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.

(12)

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.

(13)

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();

}

(14)

Output of the DateProgram.

(15)

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.

Referensi

Dokumen terkait

Design is the level of method anal- ysis in which we consider (a) what the objectives of a method are; (b) how language content is selected and organized within the

pay on the location of the invoked service and what platform / technology is being used by the service. Loose coupling is very important for SOA because a service call by

destination host, the Layer 2 address information is removed as the data is decapsulated and moved up the protocol stack to Layer 3... Getting the Data Through the Internetwork

When it is adapted to the clock skew measurement method, namely, the position of the food will resemble a cluster offset where the food search is carried out repeatedly, which aims

There was no need to call method join() in the Java programs in Listings 1.1 and 1.2 since threads created in a Java main() method continue to run after the main thread

Stack operation To facilitate passing the parameters to a subroutine and also to save state link register for recursive call, two stack operations are defined: push, pop.. r1 is used

Comparisons of previous research and the proposed method Factor Previous research Proposed method Objective Service planning Service monitoring and controlling Perspective Service

; cannot use 128-byte red zone below stack pointer because proc1 is not a leaf function, ; so proc2 can clobber the red zone subq $8, %rsp ; reserve space on stack for locals call