Java Reference
In-Depth Information
may invoke or use this method. The modifier, static , means that this method is
unique and can be invoked without creating a subclass or instance. As shown in
line 12 in Figure 2-21 on the previous page, the main() method in the Welcome
class of the Welcome to My Day program is both public and static.
A typical method header has three parts after the modifiers: a reference to
the data type of the return value, the method name, and a list of parameters. A
return value is the result or answer of a method. A method can return data —
similar to the return value of a function in a spreadsheet application — or it can
return no data. A method that returns data lists the expected data type. A
method that does not return data uses the keyword, void , instead of a data type.
The main() method in the Welcome class does not create a return value and thus
uses the keyword, void.
The method name is next in the method header. As shown in Figure 2-21,
the main() method uses the name main. The method name is followed by paren-
theses. It is easy to recognize a method name in Java because it always is followed
by a set of parentheses.
The parentheses enclose a list of parameters used by the method. A
parameter is a piece of data received by the method to help the method per-
form its operation. For example, a method to calculate sales tax would need to
know the amount of the sale and the tax rate in order to create a return value.
The sales amount and the tax rate would be parameters. Later in the chapter,
when you learn how to send data to a method, that data will be called an argu-
ment . Parameters and arguments are closely related concepts.
In the Welcome class, the main() method has one parameter named args.
The word, args, is not a keyword; it is an identifier for a piece of data that the
main() method may need. An identifier is any word you choose to name an
item in a Java program. An identifier is used to name a variable , which is a loca-
tion in computer memory that can change values as the code executes. For
example, you might use the identifier, sTax, to name a variable that holds the
state tax. Java programmers typically use the identifier, args, to name the
parameter for the main() method, although you can use other identifiers.
Variable names have the same spelling restrictions as class names (see Table 2-4
on page 65).
Each parameter must be preceded by a data type declaration. A data type
is a word that describes the type or category of data the method uses. The
parameter, args, has a String data type, which indicates a series or string of
characters. The String data type is indicated by the notation, String[] , in line 12
in Figure 2-21. You will learn more about data types in a later chapter.
When documenting information about the main() method, programmers
and language documentation would use the following terminology: Java's main()
method is public and static, accepts a String parameter named args, and returns
void.
The main() method header is placed before the lines of executable code, or
body , of the main() method. The body is enclosed in pairs of braces, just as the
body of the class is enclosed in braces. Line 13 in Figure 2-21 contains the open-
ing brace of the main() method's body. It is common to indent all lines of code
entered after the opening brace to facilitate reading.
Perform the following step to enter the method header for the main()
method, along with its opening brace.
Search WWH ::




Custom Search