Chapter 5 - Classes and methods II. In this chapter we will: formally introduce the class construct as it is used in the Java language, discuss the use of instance variables to facilitate method communication, demonstrate the use of classes to improve program structure. | Chapter 5 Classes and Methods II Lecture Slides to Accompany An Introduction to Computer Science Using Java (2nd Edition) by . Kamin, D. Mickunas, E. Reingold Chapter Preview In this chapter we will: formally introduce the class construct as it is used in the Java language discuss the use of instance variables to facilitate method communication demonstrate the use of classes to improve program structure Building Classes with Multiple Methods Computer programs can be thought of using phases Input Computation Output Using separate methods for each phase can improve the maintainability of a class or program Building Class Definitions public class classname { // Author, data, explanation declarations of instance variables public void methodName1 (parameter) { declarations of local variables executable statements with comments } public void methodName2 (parameter) { declarations of local variables executable statements with comments } } Instance Variables Local variables variables declared inside methods not accessible to any other method cannot be used for communication Instance variables declared outside the methods, but declared inside the class all class methods have access to the class instance variables can be used for communication inside class Initialization of Instance Variables Instance variable declarations can contain initializers just like local variables Unlike local variables, instance variables will be initialized to default values if no initializers are found integers and doubles are initialized to 0 characters are initialized to the null character (ASCII code 0) booleans are initialized to false object-type variables are initialized to the reference value null Hose Class Methods void getData() // Reads and stores the height and // weight data. void compute() // Computes and stores hose size. void display() // Displays the results of the // computation. UML Diagram for Hose Class Variable Scope Rules The scope of an instance variable is the entire . | Chapter 5 Classes and Methods II Lecture Slides to Accompany An Introduction to Computer Science Using Java (2nd Edition) by . Kamin, D. Mickunas, E. Reingold Chapter Preview In this chapter we will: formally introduce the class construct as it is used in the Java language discuss the use of instance variables to facilitate method communication demonstrate the use of classes to improve program structure Building Classes with Multiple Methods Computer programs can be thought of using phases Input Computation Output Using separate methods for each phase can improve the maintainability of a class or program Building Class Definitions public class classname { // Author, data, explanation declarations of instance variables public void methodName1 (parameter) { declarations of local variables executable statements with comments } public void methodName2 (parameter) { declarations of local variables executable statements with comments } } Instance Variables Local variables variables .