Java Reference
In-Depth Information
6.1
Introduction
6.2
Program Modules in Java
6.3
static
Methods,
static
Fields
and Class
Math
6.4
Declaring Methods with Multiple
Parameters
6.5
Notes on Declaring and Using
Methods
6.6
Method-Call Stack and Stack Frames
6.7
Argument Promotion and Casting
6.8
Java API Packages
6.9
Case Study: Secure Random-Number
Generation
6.10
Case Study: A Game of Chance;
Introducing
enum
Types
6.11
Scope of Declarations
6.12
Method Overloading
6.13
(Optional) GUI and Graphics Case
Study: Colors and Filled Shapes
6.14
Wrap-Up
Summary | Self-Review Exercises | Answers to Self-Review Exercises | Exercises | Making a Difference
Experience has shown that the best way to develop and maintain a large program is to con-
struct it from small, simple pieces, or
modules
. This technique is called
divide and con-
quer
. Methods, which we introduced in Chapter 3, help you modularize programs. In this
chapter, we study methods in more depth.
You'll learn more about
static
methods, which can be called without the need for an
object of the class to exist. You'll also learn how Java is able to keep track of which method
is currently executing, how local variables of methods are maintained in memory and how
a method knows where to return after it completes execution.
We'll take a brief diversion into simulation techniques with random-number genera-
tion and develop a version of the dice game called craps that uses most of the programming
techniques you've used to this point in the topic. In addition, you'll learn how to declare
constants in your programs.
Many of the classes you'll use or create while developing applications will have more
than one method of the same name. This technique, called
overloading
, is used to imple-
ment methods that perform similar tasks for arguments of different types or for different
numbers of arguments.
We continue our discussion of methods in Chapter 18, Recursion. Recursion pro-
vides an intriguing way of thinking about methods and algorithms.
You write Java programs by combining new methods and classes with predefined ones
available in the
Java Application Programming Interface
(also referred to as the
Java API
or
Java class library
) and in various other class libraries. Related classes are typically
grouped into
packages
so that they can be
imported
into programs and
reused
. You'll learn
how to group your own classes into
packages
in Section 21.4.10. The Java API provides a
rich collection of predefined classes that contain methods for performing common math-
ematical calculations, string manipulations, character manipulations, input/output opera-
tions, database operations, networking operations, file processing, error checking and
more.