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.
variables are identifiers. A variable holds a reference to some object.
instanceVariableNames: here x
belongs to an instance of a class.
classVariableNames: here Yy
have a copy of the variable shared with all instances of all classes, and it can be static a variable.
so x
can have different values across different objects. But Yy
can have only one value.
poolDictionaries: are created in smallTalk to provide access to variables shared among a group of classes
category 'helpme' is a collection of related classes, if you create your class without a category; the class gets created with a blank category.
subclass has its own instanceVariableNames(zzz), also has the inheritance property.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With