Navigation:  Tutorials > Beginners Guide to Smalltalk > Objects and Messages > Variables and Assignments >

Global Variables

Previous pageReturn to chapter overviewNext page

Global variables are similar to workspace variables except that they are owned by the system rather than a particular workspace. Once you create a global it remains in existence, holding onto any contents that you may assign to it, until you explicitly remove it. A global variable name must begin with an upper case letter.

If you execute the following code, you will be prompted as to whether you wish to create the global, MyName. Click Yes to do so.

MyName := 'Arthur J. Rank'.

MyName , ' was here'.

 

It is important to cleanup any globals that you may create after you have finished using them. To do this for MyName, evaluate the following (note the # sign immediately preceeding the name):

Smalltalk removeKey: #MyName.

 

You should find that you don't end up explicitly creating global variables very often. However, there is one sort of global that you will create regularly. Globals are used to hold the class objects that will form the basis of much of your Smalltalk programming. These are also introduced in Classes and Methods.

Smalltalk maintains quite a number of global variables of which most are classes. Another, which you'll often find useful, is Transcript. This can be used to reference the System Transcript window in order to send useful text reports to it:

10 timesRepeat: [Transcript show: 'A useful message?'; cr].

 

Check the Transcript window to see the message logged to it 10 times.