Page 318 - Introduction to Programming with Java: A Problem Solving Approach
P. 318

                284 Chapter 7 Object-Oriented Programming—Additional Details
The instance variables in Figure 7.25a expand the UML class diagram for the GarageDoorSystem
class to this:
 GarageDoorSystem
  -state : int
-control : Controller -upSwitch : Switch -downSwitch : Switch -button : Switch
 +GarageDoorSystem() +setState(state : int) : void +run(steps : int) : void
 Figure 7.25b contains the rest of the GarageDoorSystem class. This is all just one big method, the run method, which describes the garage-door operation—the system’s process. It’s a big for loop that takes a specified number of steps. At each step, the user specifies one of two types of events, either a button press or an arrival at one of the travel limits. A big do loop uses the input value to print out an appropriate message and perhaps ask for data re-entry. Delegating this detail to a subordinate class is more elegant than trying to handle it all in the main method of a driver.
Summary Apago PDF Enhancer
• When you declare a reference variable, the JVM allocates space in memory for holding a reference to an object. At that point, there is no memory allocation for the object itself.
• Assigning one reference variable to another does not clone an object. It just makes both reference vari- ables refer to the same object and gives that object an alternate name—an alias.
• To create a separate object, you must use Java’s new operator. To make a second object be like a first object, copy the first object’s instance variable values into the corresponding instance variables in the second object.
• A method can return an assortment of data originating in a method by returning a reference to an inter- nally instantiated object that contains that data.
• Java’s garbage collection program searches for inaccessible objects and recycles the space they occupy by asking the operating system to designate their space in memory as free space.
• If you compare two object references with ==, the result is true if and only if the references point to the same object.
• To see whether two different objects contain similar data, you must write an equals method that indi- vidually compares respective instance variable values.
• To swap two variables’ values, you need to store one of the variable’s values in a temporary variable.
• If you pass a reference as an argument, and if the reference parameter’s instance variables are up- dated, then the update simultaneously updates the reference argument’s instance variables in the calling
module.
• If a method returns a reference to an object, you can use what’s returned to call another method in the
 same statement. That’s method-call chaining.














































































   316   317   318   319   320