Java Reference
In-Depth Information
method should allow only valid modifications to the face value of a die. We explore
how that kind of control can be accomplished in the next chapter.
SELF-REVIEW QUESTIONS (see answers in Appendix N)
SR 4.11 Objects should be self-governing. Explain.
SR 4.12 What is the interface to an object?
SR 4.13 What is a modifier?
SR 4.14 Why might a constant be given public visibility?
SR 4.15 Describe each of the following:
a. public method
b. private method
c. public variable
d. private variable
4.4 Anatomy of a Method
We've seen that a class is composed of data declarations and method declarations.
Let's examine method declarations in more detail.
As we stated in Chapter 1, a method is a group of programming language
statements that is given a name. A method declaration specifies the code that is
executed when the method is invoked. Every method in a Java program is part of
a particular class.
When a method is called, the flow of control transfers to that method. One
by one, the statements of that method are executed. When that method is done,
control returns to the location where the call was made and execution continues.
The called method (the one that is invoked) might be part of the same class as
the calling method that invoked it. If the called method is part of the same class,
only the method name is needed to invoke it. If it is part of a different class, it is
invoked through the name of an object of that other class, as we've seen many
times. Figure 4.7 shows the flow of execution as methods are called.
We've defined the main method of a program many times in previous examples.
Its definition follows the same syntax as all methods. The header of a method
includes the type of the return value, the method name, and a list of parameters
that the method accepts. The statements that make up the body of the method
are defined in a block delimited by braces. The rest of this section discusses issues
related to method declarations in more detail.
 
Search WWH ::




Custom Search