[squeak-dev] Class variables

K. K. Subramaniam subbukk at gmail.com
Tue Jul 15 13:20:42 UTC 2008


On Monday 14 Jul 2008 6:57:10 pm Martin Beck wrote:
> Hi list,
>
> can anyone please explain the difference between class variables at the
> instance side in the class browser and instance variables at the class
> side (thus of the metaclass) to me? And which of them is the preferred one?
Read 'class variables' as 'shared variables'. On any given side, 
classVariableNames: defines variables that are shared across all instances, 
while instanceVariableNames: defines variables that are unique to each 
instance. Both these sets of variables are accessible only to the methods 
defined on that side and its sub-classes.

Classes are instances of a Metaclass but they are special in that there will 
be only one instance of a given class (stored in a global variable whose name 
is same as the class name), so there is no need for a separate 
classVariableNames: on the class side.

 Metaclass allInstances select: [ :c | c instVarNames size > 0 ]

They do come in handy to track global state. For instance, SmalltalkImage 
class uses 'current' to track its singleton instance. ProjectLoading class 
uses it to track current world being loaded. Beeper uses one for the default 
sound to be played for system beep.

Classes are global variables and their instance variables are also global. 
Unintentional modifications can result in weird side-effects (see comment in 
Player class>>scripts). So they are sparingly used.

HTH,
Subbu



More information about the Squeak-dev mailing list