Page 94 - Introduction to Programming with Java: A Problem Solving Approach
P. 94

                60 Chapter 3 Java Basics 3.4 The Class Heading
So far, we’ve focused on code that the computer ignores—comments. Now let’s talk about code that the computer pays attention to. Here’s the first non-comment line in the Dream program:
public class Dream
That line is called a class heading because it’s the heading for the definition of the program’s class. What’s a class? For now, think of a class simply as a container for your program’s code.
Let’s examine the three words in the class heading. First, the last word—Dream. Dream is the name of the class. The compiler allows the programmer to choose any name for the class, but in the interest of mak- ing your code readable, you should choose a word(s) that describes the program. Since the Dream program prints “I have a dream,” Dream is a reasonable class name.
 The first two words in the class heading, public and class, are reserved words. Reserved words, 3
also called keywords, are words that are defined by the Java language for a particular purpose. They cannot be redefined by a programmer to mean something else. That means programmers cannot use reserved words when choosing names in their programs. For example, we were able to choose Dream for the class name because Dream is not a reserved word. We would not have been allowed to choose public or class for the class name.
So what are the meanings of the public and class reserved words? The word class is a marker that signifies the beginning of the class. For now, with our simple one-class programs, the word class also signifies the beginning of the program.
The word public is an access modifier—it modifies the class’s permissions so that the class is acces- Apago PDF Enhancer
sible by the “public.” Making the class publicly accessible is crucial so that when a user attempts to run it, the user’s run command will be able to find it.
There are certain coding conventions that most programmers follow. We list such conventions in our “Java Coding-Style Conventions” appendix. Throughout the book, when we refer to “standard coding con- ventions,” we’re referring to the coding conventions found in the appendix. Standard coding conventions dictate that class names start with an uppercase first letter; thus, the D in the Dream class name is upper- case. Java is case-sensitive, which means that the Java compiler distinguishes between lowercase and upper- case letters. Since Java is case-sensitive, the filename should also start with an uppercase first letter.
3.5 The main Method’s Heading
We’ve talked about the class heading. Now it’s time to talk about the heading that goes below the class heading—the main method heading. In starting a program, the computer looks for a main method head- ing, and execution begins with the first statement after the main method heading. The main method heading must have this form:
public static void main(String[] args)
Let’s start our analysis of the main method heading by explaining the word main itself. So far, all you know about main is that in starting a program, the computer looks for it. But main is more than that; it’s a Java method. A Java method is similar to a mathematical function. A mathematical function takes arguments, performs a calculation, and returns an answer. For example, the sin(x) mathematical function
3 In Java, reserved words and keywords are the same. But in some programming languages, there is a subtle difference. In those lan- guages, both terms refer to words that are defined by the programming language, but keywords can be redefined by the programmer, and reserved words cannot be redefined by the programmer.
  


















































































   92   93   94   95   96