LECTURE NOTES SEPTEMBER 29 2004 ANNOUNCEMENTS First Midterm Exam Tuesday October 5 5:00-7:00 pm 1227 Engr Hall Different proctor Bring several #2 pencils and your UW student ID ASSIGNMENTS 10/1 Assignment 1 CodeLab 4 10/6 CodeLab 5 VARIABLE SCOPE: USING ARGUMENTS, PARAMETERS, AND LOCAL VARIABLES, CONT'D Why? - directly using parameter to initialize class variable What? - scope - where a variable is valid - may reuse identifiers among different scopes Why? - same identifier refers to multiple variables from different scopes What? - resolving names How? - first assumes identifier belongs to the most local (method) scope - if no local declaration or parameter with that identifier, assume class scope - if no class declaration with that identifier, compiler error Why? - also have a local variable or parameter by the same name - not required otherwise What? - specifying a class identifier How? this. - common practice not to use unless necessary COMMON WAYS TO USE METHODS IN CLASS DEFINITIONS Why? - properties are internal details that shouldn't be visible to clients What? - methods to get and set data members How? - *mutator* - *accessor* What? - calling a method within a method declaration How? - name directly - in conjunction with dot notation What? - calling a method within a constructor How? - call to another constructor must be the first line - cannot call instance methods before a call to a constructor What? - *overloaded method* Why? - avoids duplication - can implement two similar methods by having one call another How? - follow same rules as with constructors What? - method that calls private methods in a particular sequence Why? - not robust to rely on the client to do so USING INSTANTIABLE CLASSES What? - define a new class When? - encounter common task repeated over and over - avoids cutting and pasting What? - using classes together Why? - sometimes need more than one class in a program How? - reusable *service provider* classes - nonreusable *controller* class What? - developing classes together How? - *top-down development* - dummy service objects - *botton-up development* - dummy main class Why? - implement dummies in either development strategy What? - *stub method* Why? - never have variations with different data members What? - noninstantiable classes How? - not covered yet - OK to implement as an instantiable class ORGANIZING CLASSES INTO PACKAGES What? - using other classes without defining a package How? - place class definitions in the same folder Why? - more convenient What? - using classes in different folders How? - create a package and use the import statement What? - setting up a package for reuse How? - define the package in the first statement of the source file package ; - declare class with the visibility modifier public public class { ... } - create a folder with the same name as the package (Java requires a one-to-one correspondence between packages and folders) - place the modified class into this folder and compile - modify the CLASSPATH environment variable to include this folder