Page 95 - Introduction to Programming with Java: A Problem Solving Approach
P. 95
takes the x argument, calculates the sine of the given x angle, and returns the calculated sine of x. Likewise, a Java method may take arguments, will perform a calculation, and may return an answer.
The rest of the main heading contains quite a few mysterious words whose explanations may be con- fusing at this point. In later chapters, when you’re better prepared, we’ll explain the words in detail. For now, it’s OK to treat the main method heading as a line of text that you simply copy and paste under the class heading. We realize that some of you may be uncomfortable with that. For you folks, the rest of this section explains main method heading details.
Explanation of main Method Heading Details
We’ll now explain the three reserved words at the left of the main method heading—public
void. As previously mentioned, the word public is an access modifier—it grants permissions so that main is accessible by the “public.” Since main is the starting point for all Java programs, it must be pub- licly accessible.
While public specifies who can access the main method (everyone), the word static specifies how to access the main method. With a non-static method, you must do some extra work prior to accessing
4
it. On the other hand, a static method can be accessed immediately, without doing the extra work. Since
main is the starting point for all Java programs, it must be immediately accessible, and therefore it requires the word static.
Now for the third reserved word in the main heading—void. Remember that a method is like a math- ematical function—it calculates something and returns the calculated value. Well actually, a Java method sometimes returns a value and sometimes returns nothing. void indicates that a method returns nothing.
3.6 Braces 61
static
Since the main method returns nothing, we use void in the main method’s heading. Apago PDF Enhancer
Now for the (String[]
tion takes arguments. Likewise the main method takes arguments. Those arguments are represented by the word args. In Java, if you ever have an argument, you need to tell the computer what type of value the argument can hold. In this case, the argument’s type is defined to be String[], which tells the computer that the args argument can hold an array of strings. The square brackets, [], indicate an array. An array is a structure that holds a collection of elements of the same type. In this case String[] is an array that holds a collection of strings. A string is a sequence of characters. You’ll learn more about strings later in this chapter in Section 3.22, and you’ll learn about arrays in Chapter 10.
3.6 Braces
In the Dream program, we inserted opening braces, {, below the class heading and below the main head- ing, and we inserted closing braces, }, at the bottom of the program. Braces identify groupings for humans and for the computer. They must come in pairs—whenever you have an opening brace, you’ll need an as- sociated closing brace. In the Dream program, the top and bottom braces group the contents of the entire class, and the interior braces group the contents of the main method. For readability’s sake, you should put an opening brace on a line by itself in the same column as the first character of the previous line. Look at the following code fragment and note how the opening braces are positioned correctly.
4 To access a non-static method (more formally called an instance method), you must first instantiate an object. We describe object instantiation in Chapter 6.
5 Although the main method takes arguments, it’s rare for the main method to use those arguments. The book’s programs do not use the main method’s arguments.
args) portion of the main heading. Remember that a mathematical func- 5