Skip to content
Tutorials Center
My learning

Topics

Lesson 20 of 38 ยท Reflection and Metaclasses

Classes Are Objects and Metaclasses

Understand why every Smalltalk class is an object, what a metaclass is, how class-side methods and class-side instance variables work, and walk the class hierarchy with runnable JavaScript stand-ins.

In Smalltalk the rule "everything is an object" has no exceptions. A class such as Point is an object too, which means it must itself be an instance of some class. That class is called a metaclass. Metaclasses sound intimidating, but they exist for one practical reason: they hold the class-side methods, such as Point x: 3 y: 4, that you send to a class rather than to an instance.

This lesson explains the pattern in four short rules, shows the real answers you get in a Smalltalk image (read-only), and then rebuilds the idea with runnable JavaScript so you can see why it holds together.

The four rules

  1. Every object is an instance of a class.
  2. Every class is an object, so it is an instance of a metaclass.
  3. Each class has exactly one metaclass, created automatically with it. The metaclass of Point is called Point class.
  4. The metaclass hierarchy mirrors the class hierarchy, and the top of it joins back to the normal world: Object class inherits from Class.

You can check these in an image (read-only):

SMALLTALK
3 class.                  "SmallInteger"
SmallInteger class.       "SmallInteger class"
SmallInteger class class. "Metaclass"
Metaclass class class.    "Metaclass: the circle closes"
Object class superclass.  "Class"
Class superclass.         "ClassDescription, then Behavior, then Object"

The last two lines are the surprising part. Metaclass class is an instance of Metaclass, so the chain loops. Nothing is missing underneath; the loop is simply how Smalltalk ends the infinite regress.

Class-side methods

Because a class is an object, its behaviour lives in its metaclass. When you browse a class in Pharo, the "Class side" toggle is showing the methods of the metaclass. Typical class-side methods are constructors and singletons (read-only):

SMALLTALK
Object subclass: #Account
    instanceVariableNames: 'owner balance'
    classVariableNames: ''
    package: 'Bank'.

"class side"
Account class >> owner: aString
    ^ self new setOwner: aString

"instance side"
Account >> setOwner: aString
    owner := aString.
    balance := 0

Sending Account owner: 'Ana' runs the class-side method with self bound to the class Account. Inside, self new creates an instance. Because lookup follows the metaclass chain, a subclass such as SavingsAccount inherits owner: and self new creates a SavingsAccount. The method needs no hard-coded class name.

Class-side instance variables

A class is an object, so it can have instance variables of its own. They are called class-side instance variables. They are different from class variables, which are shared by the class and all its subclasses. Each subclass gets its own copy of a class-side instance variable, which makes them ideal for per-class state such as a registry or a singleton instance (read-only):

SMALLTALK
Account class
    instanceVariableNames: 'instances'.

Account class >> instances
    ^ instances ifNil: [ instances := OrderedCollection new ]

A runnable stand-in

JavaScript has a similar structure: a JavaScript class is a function object, static members live on that object, and a subclass inherits them through its own prototype chain. Run this to watch the two chains.

JS Stand-in: static members and the two chains

Press Run (or Ctrl + Enter) to see the console output here.

No output.

The first chain is the instance side: SavingsAccount.prototype, then Account.prototype, then Object.prototype. The second is the class side: SavingsAccount, then Account. In Smalltalk the second chain is SavingsAccount class, Account class, Object class, Class, and so on.

One difference to note: the JavaScript static registry is a single array shared by reference, because it is looked up through the chain. A Smalltalk class-side instance variable would be separate for every subclass. The next example fakes that behaviour.

JS Stand-in: per-class state with a lazy slot

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Why bother?

Understanding metaclasses removes the mystery from tools. The class browser's two toggles, the Foo class >> bar notation in stack traces and the compile-time Foo class messages in tutorials all refer to the same thing. It also explains reflection: asking Account class for its selectors lists the class-side constructors.

flowchart TB
  subgraph Instances
    I["anAccount"]
  end
  subgraph Classes
    A["Account"]
  end
  subgraph Metaclasses
    AM["Account class"]
  end
  I -->|"is a"| A
  A -->|"is a"| AM
  AM -->|"is a"| MC["Metaclass"]
  MC -->|"is a"| MCC["Metaclass class"]
  MCC -->|"is a"| MC

Every arrow reads "is an instance of". The last two close the loop.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What is the class of the class Point?

    Each class is the single instance of its own metaclass, named Point class.
  2. 2. Where do class-side methods such as Account owner: live?

    Class-side methods are stored in the metaclass; the class browser's class toggle shows them.
  3. 3. How does a class-side instance variable differ from a class variable?

    Class variables are shared across the hierarchy, while class-side instance variables are per class.
  4. 4. What is Metaclass class an instance of?

    The loop Metaclass class class == Metaclass ends the regress.

Summary

  • Classes are objects; their classes are metaclasses, one per class, created automatically.
  • Class-side methods (constructors, singletons, registries) live in the metaclass.
  • Class-side instance variables give each class its own state; class variables are shared.
  • The metaclass chain mirrors the class chain and loops at Metaclass.
  • The JavaScript snippets are stand-ins that mimic the structure; the Smalltalk code is read-only.

Next: doesNotUnderstand: and Proxies