Java Reference
In-Depth Information
In Chapter 7, you'll learn how to maintain lists and tables of data in arrays. You'll see
a more elegant implementation of the application that rolls a die 6,000,000 times. We'll
present two versions of a GradeBook case study that stores sets of student grades in a Grade-
Book object. You'll also learn how to access an application's command-line arguments that
are passed to method main when an application begins execution.
Summary
Section 6.1 Introduction
• Experience has shown that the best way to develop and maintain a large program is to construct
it from small, simple pieces, or modules. This technique is called divide and conquer (p. 201).
Section 6.2 Program Modules in Java
• Methods are declared within classes. Classes are typically grouped into packages so they can be
imported and reused.
• Methods allow you to modularize a program by separating its tasks into self-contained units. The
statements in a method are written only once and hidden from other methods.
• Using existing methods as building blocks to create new programs is a form of software reusabil-
ity (p. 202) that allows you to avoid repeating code within a program.
Section 6.3 static Methods, static Fields and Class Math
• A method call specifies the name of the method to call and provides the arguments that the called
method requires to perform its task. When the method call completes, the method returns either
a result, or simply control, to its caller.
• A class may contain static methods to perform common tasks that do not require an object of
the class. Any data a static method might require to perform its tasks can be sent to the method
as arguments in a method call. A static method is called by specifying the name of the class in
which the method is declared followed by a dot ( . ) and the method name, as in
ClassName . methodName ( arguments )
• Class Math provides static methods for performing common mathematical calculations.
• The constant Math.PI (p. 204; 3.141592653589793) is the ratio of a circle's circumference to its
diameter. The constant Math.E (p. 204; 2.718281828459045) is the base value for natural loga-
rithms (calculated with static Math method log ).
Math.PI and Math.E are declared with the modifiers public , final and static . Making them
public allows you to use these fields in your own classes. A field declared with keyword final
(p. 205) is constant—its value cannot be changed after it's initialized. Both PI and E are declared
final because their values never change. Making these fields static allows them to be accessed
via the class name Math and a dot ( . ) separator, just like class Math 's methods.
• All the objects of a class share one copy of the class's static fields. Together the class variables
(p. 204) and instance variables represent the fields of a class.
• When you execute the Java Virtual Machine (JVM) with the java command, the JVM loads the
class you specify and uses that class name to invoke method main . You can specify additional
command-line arguments (p. 205) that the JVM will pass to your application.
• You can place a main method in every class you declare—only the main method in the class you
use to execute the application will be called by the java command.
Search WWH ::




Custom Search