Smalltalk has six kinds of variables - temporary, instance, class, class instance, global, pool.

Each variable type has its own scope and lifetime. Everything is "pass by reference" in Smalltalk but there's no pointer concept. Instead, a variable has an object.

An important aspect is that Smalltalk doesn't have type for any variable. If you learned Basic language a little bit, you must have an experience to declare a variable without type. Just like that, we don't need to specify any type to declare a variable in Smalltalk. All we need to do is to assign that variable any kind of value. That's it. It is string variable when we assign it a string. It is array when we assign it an array. This characteristic of smalltalk becomes possible by the concept of polymorphism. Instead of a specific type, there exists an appropriate class. This makes Smalltalk unique from languages to use classes and types together like C++. Surely, this kind of classes can be overridden by user.

Temporary Variables

A temporary variable is a kind of local variable used only within a method. Therefore, this variable lives while the method's executing. A temporary variable must begin with lower caps and must be defined within two '|'s when a method definition begins.

| anApple semglMember allther |

Above is an example to declare anApple, semglMember and allther as temporary variables. As mentioned before, there's no type has been defined for each variable. Precisely, a smalltalk variable can be assigned by any kind of objects. For instance, we can assign a variable string class which had integer class before. All these variables have nil, initially.

Instance Variables

Instance variables are encapsulated inside objects. Thus, the lifetime of an instance variable is same with its object. An instance variable begins with lower caps. An instance variable is privately owned by an object and can only be used by the methods of the object. An instance variable is declared in the object's class where the instance variable belongs to. Every instance of a class has instance variables with same names but they are stored in different location of memory. Therefore, they're independent to each other.

Visualworks/Smalltalk or GNU Smalltalk/X provide class browser and we can declare instance variable through it. Smalltalk-80 which is usually bundled with a Linux distribution provides a little bit complex way of declaring a class.
(Actually, this complex way is the standard. Advanced development environments just wrap it pretty.)

Class Variables

Class variables are very similar to instance variables. The only difference is ownership. A class variable belongs to a class. Not just the class which owns a class variable but instances of the class can also use it. A class variable is stored in one location of memory since it doesn't exist differently on each instance. So, methods of same class handles same class variable. A class variable begins with capital letter.

Class-Instance Variables

A class-instance variable is declared to use within a class just like a class variable. These two variable types differ by inheritance. A class variable declared in a super class can be used in a child class of it but a class instance variable cannot be inherited. An instance variable is declared per each instance of a class while a class variable can be shared by both of instances of current class and instances of child class but a class instance variable can be used only within current class. In fact, class instance variable is not as popular as others. It also begins with capital letter like class variables.

Global Variables

A global variable can be used anywhere once it's declared. The declaration of a global variable is simply done by referring it or sending the Smalltalk object a message. A global variable always begins with capital letter.

Pool Variables

Pool variable can be used various places like global variable. The declaration of this variable is not as simple as a global variable but it's declared with a class definition which specifies where it'll be used. Pool variable is bound with Dictionaries. Pool variables which will be used by a class must be specified in the poolDictionaries item when the class is declared.

See following class defition

Object subclass: #Square
   instanceVariableNames: 'size colour'
   classVariableNames: 'Material'
   poolDictionaries: 'Shapes'

In above, Shapes is dictionaries having pool variables that #Square class will use.

Table of Contents

Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License