Good Programming Practice 2.2 Use lines and space characters to enhance program readability. Line 4 public class Welcome1 begins a declaration for class Welcome1. Every program in Java consists of at least one class declaration that is defined by youthe programmer. These are known as classes or user-defined classes. The class keyword introduces a class declaration in Java and is immediately followed by the class name (Welcome1). Keywords (sometimes called reserved words) are reserved for use by Java (we discuss the various keywords throughout the text) and are always spelled with all lowercase letters. By convention, all class names in Java begin with a capital letter and capitalize the first letter of each word they include (e.g., SampleClassName). A Java class name is an identifiera series of characters consisting of letters, digits, underscores (_) and dollar signs ($) that does not begin with a digit and does not contain spaces. Some valid identifiers are Welcome1, $value, _value, m_inputField1 and button7. The name 7button is not a valid identifier because it begins with a digit, and the name input field is not a valid identifier because it contains a space. Normally, an identifier that does not begin with a capital letter is not the name of a Java class. Java is case sensitive that is, uppercase and lowercase letters are distinct, so a1 and A1 are different (but both valid) identifiers. Good Programming Practice 2.3 By convention, always begin a class name's identifier with a capital letter and start each subsequent word in the identifier with a capital letter. Java programmers know that such identifiers normally represent Java classes, so naming your classes in this manner makes your programs more readable. Common Programming Error 2.2 Java is case sensitive. Not using the proper uppercase and lowercase letters for an identifier normally causes a compilation error. class we define begins with the public keyword. For now, we will simply require this keyword. When you save your public class declaration in a file, the file name must be the class name followed by the ".java" file-name extension. For our application, the file name is Welcome1.java. Common Programming 2.3
to method displayMessage. When method displayMessage completes its task, method main continues executing at line 14. This is the end of method main, so the program terminates. UML Class Diagram for Class GradeBook Instance Variables, set Methods and get Methods In , we declared all of an application's variables in the application's main method. Variables declared in the body of a particular method are known as local variables and can be used only in that method. When that method terminates, the values of its local variables are lost. Recall from that an object has attributes that are carried with the object as it is used in a program. Such attributes exist before a method is called on an object and after the method completes execution. A class normally consists of one or more methods that manipulate the attributes that belong to a particular object of the class. Attributes are represented as variables in a class declaration. Such variables are called fields and are declared inside a class declaration but outside the bodies of the class's method declarations. When each object of a class maintains its own copy of an attribute, the field that represents the attribute is also known as an instance variableeach object (instance) of the class has a separate instance of the variable in memory. The example in this section demonstrates a GradeBook class that contains a courseName instance variable to represent a particular GradeBook object's course name. GradeBook Class with an Instance Variable, a set Method and a get Method In our next application (), GradeBook () maintains the course name as an instance variable so that it can be used or modified at any time during an application's execution. The class contains three methodssetCourseName, getCourseName and displayMessage. Method setCourseName stores a course name in a GradeBook. Method getCourseName obtains a GradeBook's course name. Method displayMessagewhich now specifies no parametersstill displays a welcome message that includes the course name. However, as you will see, the method now obtains the course name by calling another method in the same classgetCourseName. Figure 3.7. GradeBook class that contains a courseName instance variable. Access Modifiers public and private