I need help understanding the usage and the difference of variables in Smalltalk. What is the difference and the usage of each variable in the given code below?
Object subclass: #MyClass
instanceVariableNames: 'x'
classVariableNames: 'Yy'
poolDictionaries: ''
category: 'helpMe'
MyClass class
instanceVariableNames: 'zzz'
An instance variable (
x
) is a variable that is local to an instance. Neither the class nor any other instance can access that variable.A class variable (
Yy
) is local to a class, all its instances, all subclasses and all subinstances (so the entire hierarchy). Any subclass or subinstance can see the value of that variable.A class instance variable (
zzz
) is local to a class. Only the class that defines the variable has access to it, neither instances nor subclasses can see the variable (although subclasses inherit the declaration of the variable, their variable will have a different value). Classes are also objects in Smalltalk. So you can think of a class instance variable the same way you would think about an instance variable: no other instance (instance of a class) can see the value. Thanks to @Amos M. Carpenter for pointing this out.