next up previous
Next: Program Walkthrough: Method Definition Up: In the Laboratory: Editing, Previous: Program Walkthrough: The import

Program Walkthrough: Class Definition

The next element in the program is the header of the class definition:


public class FirstApplet extends Applet implements ActionListener


This is a more complex class header than the one we used for the HelloWorld or Rectangle classes. But this applet is a more sophisticated program because it interacts with the user. Don't worry at this point if its operations seem a bit magical.

The class header serves the purpose of naming the class FirstApplet, designating its accessibility public, specifying that it is an Applet, and declaring that it implements ActionListener, an interface that allows it to respond to the user's mouse clicks. The header begins the definition of the class, which extends all the way to the last line of the program--the line marked with the //End of FirstApplet comment.

The body of a class definition is a block, as is the body of a method. Note how the statements in the block are indented, and how the braces are aligned and commented. These style conventions serve to make the program more readable.


Programming Tip: Use of Indentation. The code within a block should be indented, and the block's opening and closing braces should be aligned. A comment should be used to mark the end of a block of code.

Following the header is a variable declaration:


private Button clickMe;      // The button


As we will see in more detail in the next chapter, variables are memory locations that can store values. In this case, the Applet object is storing a reference to a Button object.


Programming Tip: Choice of Variable Names. Names chosen for variables, methods, and classes should be descriptive of the element's purpose and should be written in a distinctive style to distinguish them from other elements of the program. For variable and method names, our convention is to start the name with a lowercase letter and use uppercase letters to distinguish words within the name--for example, clickMe. Class names begin with an uppercase letter (FirstApplet).


next up previous
Next: Program Walkthrough: Method Definition Up: In the Laboratory: Editing, Previous: Program Walkthrough: The import
Ralph Morelli {Faculty} 2002-03-02