Home Segments Index Top Previous Next

263: Sidetrip

Initialization can be done in instance-method initializors that are incorporated into the new class method. For example, suppose that you want the initial value of each Food instance variable to be 0, rather than nil. You first define an initializor instance method that sets values to 0:

Food method definition • instance 
initialize 
  self fCalories: 0; cCalories: 0; pCalories: 0. 

Next, you incorporate the initialize instance method into the new class method:

Food method definition • class 
new 
  ^ super new initialize 

Then, all new Food instances have instance variables set to 0:

Workspace
Transcript show: Vegetable new tCalories printString 
Transcript 
0