An instance variable is state associated with an individual object. A class defines the possible structure, and each object receives its own attribute values at runtime. The current combination of those values characterizes the object’s state; changing one or more values produces a state transition. Methods operate on instance variables and help determine object behavior. section 1.8
Instance variables support several design decisions:
- Encapsulate object state inside a class, exposing public attributes through its interface while restricting private attributes to class methods. section 1.8
- Represent a class’s owned relationships: Catalogue has private _booklist containing zero or more Book objects, whereas method parameters can create only transient dependencies. section 4.3.1
- Translate requirements into object state: Catalogue stores _booklist, Book stores _attributes, Attributes stores _dictionary, and Form stores individual input values. section 3.8.1
- Record the values that drive explicit state machines, such as a ticket machine’s count, card_inserted, and card_validity variables. section 13.1
Private state is not automatically immutable
An Employee object can store _employee_id, _name, and _birthdate as private instance variables. However, assigning the caller’s Date object directly to _birthdate lets the caller change the employee’s state; returning that same object through birthdate creates another mutation path. Copy the Date object when constructing Employee and when returning birthdate to protect the embedded state. section 5.7
Table 3.2 The initial classes of the application
| Class |
State |
Instance variables |
|
|
List of books |
|
|
|
Book attributes |
|
|
|
Attribute values |
|
|
|
Input values |
Individual instance variables for the book attributes |
Use the class-state table to compare how instance variables represent a catalogue, book, attribute collection, and form. section 3.8.1
Figure 4.6 Diagrams that show more design detail. Class Catalogue depends on class Book via its private booklist instance variable. The multiplicity indicators show that a single (1) Catalogue object can contain zero or more (*) Book objects.
Use the UML figure to see a private instance variable expressing Catalogue’s dependency on Book and its zero-or-more multiplicity. section 4.3.1



