Quick Index
Index
Ivar Concept Sketches


Instance variables (ivars) are variables on a given object instance.

Recall that from one class we construct many object instances.

Ivars are typically considered private to the object.

Our simple Circle class (defintion). We define one ivar "r" (the radius of the circle).
Now we construct many Circle object instances.

One circle has r=1, another r=2, etc.

Ivar Access Example Using 'this'


This class has two ivars (width and height).

We want to access the ivars for use.

Ivars are generally private so access would only be by the object that owns them.
Accessing ivars is done similarily to sending messages.
We have a general pattern we can always use to access an ivar:
target-dot-ivar

Syntax may vary slightly between programming languages.


Here is a pattern for accessing ivars:

<target><dot><ivar>

It is very similar to our previous pattern for sending messages:

<target><dot><msg>()

target = the target (receiver) object owning the ivar
dot = .
msg = the ivar name
Using "target-dot-ivar" we write code to access an ivar:
this.width


Where:
target = "this"
dot = .
ivar = "width"