Navigation:  Tutorials > Beginners Guide to Smalltalk > Classes and Methods >

Named Instance Variables

Previous pageReturn to chapter overviewNext page

I've already mentioned that objects use instance variables to store their data and that it is an object's class that has knowledge of these instance variables. If you go back to the class definition for Point (click on the Class Definition tab in the browser) you'll see, amongst other things, that it defines the instance variable names for its instances.

ArithmeticValue subclass: #Point

       instanceVariableNames: 'x y '

       classVariableNames: ''

       poolDictionaries: ''

 

We'll look at other aspects of this definition in the next chapter but, for the moment, let's just concentrate on how the instance variable names are specified. You can see that this is done using a string containing a list of the variable names separated with spaces. As we saw in a previous example points have two instance variables called x and y.

Like all Smalltalk variables, named instance variables are just slots where other objects can be stored. They are not "typed" and therefore not constrained to holding particular classes of object. They are also only directly accessible from methods within the class itself (or its subclasses, which we'll talk about later).

Tip: this is a very important precept in Object Oriented Programming; that the internal format of the data of an object must remain hidden at all costs from the users of that object. When you want to know something about an object you send it messages. This allows the implementer of the object the flexibility to change its internal representation at some point in the future and, providing that the set of messages it responds to remains the same, nothing will be broken by such a change.