Navigation:  Tutorials > Beginners Guide to Smalltalk > Programming in Smalltalk > Creating a class variable to hold the knowledge base >

Initialize the Animal class

Previous pageReturn to chapter overviewNext page

The next job is to initialize the KnowledgeBase so that it has a suitable root object for the game to start with. If we add this initialization as part of a class method then we can re-use it when we want to clear down the game and start afresh (i.e. forget all the game has learnt). So, add the following class method to Animal and place it in the initializing category.


       "Initialize the Animal game knowledge base.

       To start with we only know about Dogs"


       KnowledgeBase := self name: 'Dog'


As the method suggests, we'll start off with the game only knowing about dogs.

Now, in a workspace, evaluate the following to actually perform the initialization.

Animal initialize.


Tip: The inclusion of an #initialize class method has a special additional significance for when you choose to save out your work so it can be imported into someone else's Dolphin image. As a class is loaded into a new image the system checks to see if an #initialize class method exists and, if it does, this is executed automatically. Consequently, this is a great way to ensure that class variables (and suchlike) are properly set up.