Home Segments Index Top Previous Next

258: Sidetrip

A full understanding of class methods requires you to understand the concept of metaclass and a bit of behind-the-scenes Smalltalk magic.

When a class is defined, Smalltalk defines a companion metaclass. Each class is an instance—the sole instance—of its metaclass. Metaclasses have no names.

Thus, the Food class is not only a direct subclass of the Object class, but also the instance of an automatically generated metaclass:

*--------------* 
| Object class | 
*--------------* 
  |              
  | subclass     
  v              
*--------------*     instance     *-------------------------------* 
| Food class   | <--------------- | Food class's metaclass        | 
*--------------*                  *-------------------------------*