5.2. Object variables

Classes and instances must be able to hold state. TOM provides a few kinds of state.


implementation class StatefulCounter
{
  int starting_value;
}

end;

As with the Counter instance defined previously, we've defined a StatefulCounter, which possesses state. In this case though, we've created a class variable, which maintains a single value, accessible by all instances of that class. However, each sub-class of StatefulCounter would receive its own copy of starting_value that would be shared by its instances.

5.2.1. Qualifiers

Various qualifiers may be applied to the declaration of an object variable. These qualifiers alter the behavior and scope of that variable.

5.2.1.1. mutable


mutable int starting_value;

A mutable variable automatically defines a setter method of the form:


void
  set_starting_value int _new_value
{
  starting_value = _new_value;
}

The compiler will generate the method using the correct type and variable names. This may be used in conjunction with the public qualifier.

5.2.1.2. obsolete


obsolete int starting_value;

An obsolete variable has been marked to warn programmers that it will be going away in the future. References to this variable will generate warnings. Additionally, if it is public or mutable, the methods generated will also be flagged as obsolete and use of those methods will generate warnings.

This does not completely work yet. Variable references do not yet generate warnings.

5.2.1.3. private


private int starting_value;

A private variable is only accessible to methods on the class which defined it.

5.2.1.4. protected


protected int starting_value;

A protected variable is only accessible to methods on the class which defined it, as well as any sub-classes.

5.2.1.5. public


public int starting_value;

The compiler automatically creates an accessor method for a public variable. The accessor is of this form:


int
  starting_value
{
  = starting_value;
}

The compiler will generate the method using the correct type and variable names. This may be used in conjunction with the public qualifier.

5.2.1.6. static


static int starting_value;

A static variable only exists upon the class which defined it. Sub-classes do not receive their own copy of the variable. The static qualifier is only valid on class variables.

5.2.1.7. local


static local int starting_value;

A local variable's value is stored within thread-local storage. The local qualifier is only valid on static class variables.

5.2.1.8. redeclare


redeclare long starting_value;

Redeclaring a variable allows the programmer to change the type. Redeclaring a variable that was not previously declared is not allowed.