Java Reference
In-Depth Information
Chapter 2
Methods
OBJECTIVES
• Learn how to call methods based on their specifications.
• Learn how to design and write methods.
• Learn how to execute a method call, using a “model of memory”.
• Study a methodology for writing method bodies called stepwise refinement
or top-down programming .
• Learn how to conditionally execute a statement.
• Learn how to repeatedly execute a statement.
INTRODUCTION
Methods —functions, procedures, and constructors— were introduced in Sec.
1.3.3. In this chapter, we describe methods in depth, showing not only how to
write them and to call, or invoke, them but also how they are executed by the
computer. We will discuss two kinds of Java methods: instance methods and stat-
ic methods.
2.1
Java methods are recipes
Cookbooks are filled with recipes. A recipe is a set of instructions for a cook to
carry out. Java classes are like cookbooks, and Java methods are like recipes.
Each method is a sequence of instructions for a computer to carry out.
Activity
2-1.1
Invoking a recipe or method
A recipe may require the use of another recipe. For example, a chocolate
cake recipe may contain the instruction
Use White icing, page 250.
When a cook reaches that instruction when making chocolate cake, they pause,
Search WWH ::




Custom Search