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. Class with an Instance Variable, a set Method and a get Method GradeBook In our next application (), () maintains the course name as an instance variable GradeBook so that it can be used or modified at any time during an application's execution. The class contains three methodssetCourseName, and Method getCourseName displayMessage. stores a course name in a Method obtains a setCourseName GradeBook. getCourseName course name. Method now specifies no parametersstill GradeBook's displayMessagewhich 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. class that contains a instance variable. GradeBook courseName Access Modifiers and public private
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
oopchap1.docx