December 21, 2024

Smalltalk Variables

 

Learning Works is a programming environment for the Smalltalk language. We use this language to program objects. In order for the LWs environment to identify an object, that object must be referenced.

A literal object, such as a number, string, character or symbol, can be referenced quite simply. For example:

100. "A reference to a SmallInteger object"
'The Open University' "A reference to a ByteString object"
$M "A reference to a Character object"
#M206 "A reference to a ByteSymbol object"

However, many objects will require the programmer to assign them a name, called a variable. How we accomplish this will be revealed later. A variable is not an object, it is a means of referencing an object.

When we create an object, or an instance of a class, it has attributes. It requires these in order to have memory, or state. State is the values of all its attributes and, in order to store this data, an object must hold references to the objects that comprise the state. The references held by an instance are called instance variables. An object can reference other objects by means of these internally stored variables. We can say that this object "knows" about other objects. However, any variables that reference it will not be "known" by the object because a variable is not an object. The use of "knows" is an example of anthropomorphism, a concept widely used in OOP.

When an object receives a message, the first thing that the Smalltalk system must determine is the receiver of the message. Variables enable the Smalltalk system to identify the receiver.

A variable can only reference one object at a time, but the same object can have more than one variable referencing it. Furthermore, if an object has no reference to it, Smalltalk automatically destroys it. This process is called garbage collection and automatically reallocates memory for use by an application.

There are other types of variables, which I will introduce when needed.

Next page » Messages

Previous page « Hierarchy and Inheritance

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Up to top of page