We have created a class, but it isn't ready to do any work for us—we have to define some messages which the class can process first. We'll start at the beginning by defining methods for instance creation:
Account class extend [ new [ | r | <category: 'instance creation'> r := super new. r init. ^r ] ]
The important points about this are:
Account classmeans that we are defining messages which are to be sent to the Account class itself.
<category: 'instance creation'>is more documentation support; it says that the methods we are defining supports creating objects of type Account.
new [and ending with
]defined what action to take for the message
new. When you enter this definition, gnu Smalltalk will simply give you another prompt, but your method has been compiled in and is ready for use. gnu Smalltalk is pretty quiet on successful method definitions—but you'll get plenty of error messages if there's a problem!
If you're familiar with other Smalltalks, note that the body of the method is always in brackets.
The best way to describe how this method works is to step through it. Imagine we sent a message to the new class Account with the command line:
Account receives the message
new and looks up
how to process this message. It finds our new definition, and
starts running it. The first line,
| r |, creates a local
r which can be used as a placeholder for
the objects we create.
r will go away as soon as the message
is done being processed; note the parallel with
goes away as soon as the object is not used anymore. And note that
here you have to declare local variables explicitly, unlike what
you did in previous examples.
The first real step is to actually create the object.
r := super new does this using a fancy trick.
super stands for the same object that the message
new was originally sent to (remember? it's
except that when Smalltalk goes to search for the methods,
it starts one level higher up in the hierarchy than the current
level. So for a method in the Account class, this is
the Object class (because the class Account inherits from is
Object—go back and look at how we created the Account
class), and the Object class' methods then execute some code
in response to the
#new message. As it turns out, Object
will do the actual creation of the object when sent a
One more time in slow motion: the Account method
wants to do some fiddling about when new objects are created,
but he also wants to let his parent do some work with
a method of the same name. By saying
r := super new he
is letting his parent create the object, and then he is attaching
it to the variable
r. So after this line of code executes,
we have a brand new object of type Account, and
is bound to it. You will understand this better as time
goes on, but for now scratch your head once, accept it as a
recipe, and keep going.
We have the new object, but we haven't set it up correctly.
Remember the hidden variable
balance which we saw
in the beginning of this chapter?
super new gives us the
object with the
balance field containing nothing, but we want
our balance field to start at 0. 1
So what we need to do is ask the object to set itself up.
r init, we are sending the
message to our new Account. We'll define
this method in the next section—for now just assume that
init message will get our Account set up.
Finally, we say
^r. In English, this is return what
r is attached to. This means that whoever sent to Account
new message will get back this brand new account. At
the same time, our temporary variable
r ceases to exist.
 And unlike C, Smalltalk
draws a distinction between
is the nothing object, and you will receive an error if you
try to do, say, math on it. It really does matter that we
initialize our instance variable to the number 0 if we wish
to do math on it in the future.