Class variables

Dan Ingalls DanI at
Sun Feb 28 04:20:46 UTC 1999

>when the class button is selected in the browser, but no method is being
>viewed, the method text field says
>Some class
>  instanceVariableNames: ''
>the instanceVariableNames are apparently something other than the
>classVariableNames listed when viewing the class definition over on the
>instance side of things (because names listed in the latter do not appear in
>the former and vice versa).
>so what is the difference between a class variable and a class instance

David -

Good question!!

A class variable is be shared by all subclasses.

A class instance variable will be separately instantiated in every instance of that metaclass or subclass thereof, and thus in all subclasses.

This tends to just be confusing in the abstract, so let's get concrete:

Imagine a class variable called 'LabelHeight' in a superclass of many window clases - it would be shared by all the subclasses, and thus be a useful, relatively constant parameter for a whole family of classes.

Similarly, imagine a class instance variable called 'layout', which might be an array of rectangles, in the same parent window class.  It would provide an extra slot in all the subclasses, in which could be stored the appropriate default pane rectangles for that superclass.

A class variable is like a local global -- it's local to a tree of subclasses, but within that scope it is a single shared global.

A class instance variable defines a new slot in that class and all its subclasses.  It's the kind of place you would want to store allInstances if you wanted to have rapid access to them.

Hope this helps.

If this is making your head spin, don't feel bad.  I never really understand anything until the second or third time it gets explained.

	- Dan

More information about the Squeak-dev mailing list