Next: , Previous: Documenting the class, Up: Creating classes

6.4.3 Defining a method for the class

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.

The important points about this are:

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 new

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 variable named 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 balance, which 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. The line r := super new does this using a fancy trick. The word super stands for the same object that the message new was originally sent to (remember? it's Account), 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 #new message.

One more time in slow motion: the Account method #new 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 r 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. By saying r init, we are sending the init message to our new Account. We'll define this method in the next section—for now just assume that sending the 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 the new message will get back this brand new account. At the same time, our temporary variable r ceases to exist.


[1] And unlike C, Smalltalk draws a distinction between 0 and nil. nil 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.