Next: Initial Values Up: Creating Classes Previous: Initialization Variables

## Instance Variables

Each instance-variable-clause declares a number of instance variables for instances of the class, or expressions to be evaluated when an instance of the class is created. The first part of a clause is the clause specifier, one of `sequence`, `public`, `private`, etc. A clause specifier determines the properties of instance variables declared in its clause:

- `sequence` does not declare any instance variables. It only specifies expressions to be evaluated (in order) when an instance of the class is initialized.
- `public` declares fresh instance variables that are visible outside the class. A derived class can override a `public` declaration, but a `public` declaration never overrides an existing declaration in the superclass.
- `override` is like `public`, but an `override` declaration always overrides an existing declaration in the superclass. A derived class can override the instance variable again.
- `private` declares instance variables that can be accessed only within the class expression. They cannot be overridden in a derived class and do not override declarations in the superclass.
- `inherit` declares instance variables that must be defined in the superclass or one of its ancestors (as `public`). An `inherit` declaration can be overridden in derived classes.
- `rename` is similar to `inherit`, but a new name is always used to access the value locally and the reference cannot be overridden. The new name can only be used within the class definition. A `rename` declaration accesses the superclass-defined variable value, even if the variable is overridden.

Each clause specifier can be used for any number of clauses in any order within a single `class*/names` expression.

The collection of instance variable declarations induces two sets of variables:

- The internal instance variables bound within the `class*` expression: this is the collection of all instance-variables, internal-instance-variables, and inherited-variables. Along with the variables for initialization variables, this-variable, and super-init-variable, all of these variables must be distinct.
- The external instance variables visible outside the `class*` expression (for derived classes or reference via `ivar`): this is the collection of all instance-variables and external-instance-variables. All of these variables must be distinct.

The same identifier can be used as an internal variable and an external variable, and it is possible to use the same identifier as internal and external variables for different bindings (as long as all internal variables are distinct and all external variables are distinct).

For `public`, `override`, and `private` instance variables, the instance-var-initial-value-expr expression provides a value for the variable in an object; when an initial value expression is not given, `(void)` is used. The process for evaluating initial value expressions is described in section 6.3.3 and section 6.4.

For each `public` instance variable defined in a class, the superclass (or any of its ancestors) must not contain a declaration for the variable. For each `override` instance variable, the superclass must already contain a declaration. These properties are verified when the `class*/name` expression is evaluated; if a `public` variable is found in the superclass or a `override` variable is not found, the `exn:object` exception is raised.

For `inherit` and `rename` instance variables, the inherited-variable or external-inherited-variable specifies a (public) instance variable from the superclass. This inheritance is verified when the `class*/name` expression is evaluated; if an inherited instance variable is not found in the superclass (or one of its ancestors), the `exn:object` exception is raised. The process that gives values to inherited variables is described in section 6.4.

Next: Initial Values Up: Creating Classes Previous: Initialization Variables