Every object in Smalltalk is an instance of some class. For example, we
might have an instance p
of the class Point
. Or
3.14 is an instance of the class Float
.
Classes in Smalltalk are objects themselves, and understand certain
messages, just like any other object. One often-used message is
new
, to make a new instance. A variant is new:
(used for e.g. the class Array). Another common kind of message is one
that gets a constant, for example Float pi
.
In the Smalltalk browser we can see methods that define class messages by clicking the "class" button instead of the "instance" button. Often built-in classes contain a category "examples" under the "class" messages.
p = new Point(10,20);The constructor is similar to, but not the same as, an ordinary method.
Point is not itself a runtime object in Java. For example, you can't pass a class as a parameter to a method, or assign it to a variable. (Java's Reflection API does let you accomplish this, but in a less direct way.)
Let's ease toward exactly what classes are in Smalltalk-80. A bit of history: in Smalltalk-76, classes were runtime objects, and were part of the normal class-instance scheme. To make an uninitialized instance of the class Point:
p := Point new.p understands messages like "+" and "x" and "printOn:" The definitions of these are in the class Point, which is itself an object.
Rule: to look up a message name, look in your class. If it's there, use it; otherwise search up the superclass chain. In other words, go up exactly one instance link, and then up 0 or more superclass links.
What is the class Point? In Smalltalk-76 it was an instance of the class Class. Class is a subclass of Object, and an instance of itself. Class understands messages like "new", "instvars", and "compile:" Consequence: all classes understand the same messages!
Thus in Smalltalk-76 initialization was done like this:
p := Point new. p x: 10 y: 20. or p := Point new x: 10 y: 20.
In Smalltalk-80, the designers wanted to allow class-specific initialization messages, e.g.
p := Point x: 10 y: 20.To accomplish this, every class is an instance of a unique metaclass. "Point" is an instance of "Point class" (the metaclass for point). Point class defines messages such as
x:y:
.
Another benefit of this is that it gives the programmer a place to hang
constants and such, e.g. Float pi
3@4 is an instance of Point Point is an instance of Point class Point class is an instance of Metaclass Metaclass is an instance of Metaclass class Metaclass class is an instance of MetaclassNote that this instantiation hierarchy is separate from the inheritance hierarchy:
Point -> Object -> ProtoObjectIn other words:
Point is a subclass of Object Object is a subclass of ProtoObjectOr a more deeply nested inheritance hierarchy:
Pubah -> Robot -> ImageMorph -> Morph -> Object -> ProtoObject
Inheritance (subclassing) among the metaclasses exactly mirrors the inheritance relationships among the classes. For example:
Point class -> Object class -> ProtoObject classIn other words:
Point class is a subclass of Object class Object class is a subclass of ProtoObject classIn the Smalltalk environment, the class/instance switch in the browser lets one switch between methods the instances understand and methods the class understand (i.e. methods defined in the class and methods defined in the metaclass). The cost in confusion can be huge, though. A particular problem is that new programmers are hit over the head with this right away (unless the teacher glosses over it, which is the usual pedagogic technique).
By far the most acute learnability issue: metaclasses.
Other issues: