Programming and Problem Solving with Java

(やまだぃちぅ) #1

(^272) | Object-Oriented Software Design and Implementation
Figure 6.1 An Analogy Illustrating the Relationships among a Class, a Variable, and an Object


.

.

.

//Then we can instantiate an object of the class Name with new
//and assign it to the variable
testName = new Name();

Think of the class definition as analogous to a set of blueprints. A blueprint isn’t a house,
but it tells you how to construct a house. A class isn’t an object, but it tells the computer how
to construct an object. Declaring a variable is like putting an empty page in your address book
in anticipation of having an address for the house. You don't yet have the address because
the house hasn't been built.
The newoperator invokes the constructor method for the class; it is analogous to calling
in a construction crew to interpret the blueprints and actually build the house. Once the
house is built on a lot, it has an address, and you can then write this address on the blank
page in your address book (assign the address of the object to the variable). Figure 6.1 illus-
trates this analogy.
Free download pdf