Class Variables

Chapter: Additional Features
...Section: Class Variables

The addition of class variables, also known as static class variables to our object system is best motivated in terms of an example. Consider a bank account class which implements the usual methods such as deposit, withdraw, balance?, set-interest, add-interest. Certain of these methods are operations which apply to single bank account objects (deposit, withdraw, and balance?) while others are more global in nature and would always be applied to all bank account objects. For example, if we assume the interest rate is the same for all accounts, this value belongs to the class, rather than any particular account. Then when we change the interest rate using set-interest, the new rate becomes visible to all objects of the class simultaneously. It makes sense, then, that a single interest rate variable should be shared by all bank account objects, and updated by a class-level method. The interest rate variable should then be called a class variable or static class variable, and the method used to update it a class method.

Class variables are shared by all instances of a particular class. Thus when one object sets that variable, all other objects which use that variable use the new value. As a second example of "class-level" activity, the bank might want to keep track of the total number of transactions and the total volume of money deposited or withdrawn from all accounts. These too should be modeled as static class variables. While private class variables maintain the internal state of each object, class variables maintain a global state for all objects of a particular class.

In order to implement static class variables, we need to make modifications to the object oriented system we have been using. We will call these Scheme extensions Scheme+-. To use this you will need to load the file spm.ss.

Now when we define a class, we will specify the class variables which are common to all instances as a separate list, followed by the usual private variables and class methods. Naturally, if the class has no class variables, this new list is empty. The syntax is as follows:

(define My-Class
  (class (... class parameters ...)
    ([cvar1 value1] ;-- these are the class variables
     [cvar2 value2]
         ...
     [cvarN valueN])
    ([var1 value1] ;-- these are the usual instance variables
     [var2 value2]
         ...
     [varN valueN])
    (Object)  ;-- this is the superclass specification
    ([meth1
       (method (a1 a2 ... aN)
         ... method body ...)]
     [meth2
       (method (a1 a2 ... aN)
         ... method body ...)]
     ... more methods ...
     [methN
       (method (a1 a2 ... aN)
         ... method body ...)])))

Here is the bank account class with two class variables added: num-transactions and vol-transactions which keep track of the number of transactions and the total amount of money moving in and out of all bank accounts. We have also made interest-rate, bank-code and output class variables. The implementation may be found in the file bank2.ss.

We now have static variables which maintain the same value for all instances of this class, but we have not made use of them in our methods. The use of these class variables is left as an exercise.


Exercise 8

Make a copy of the file bank2.ss so you can modify the definition of the Account class. Call this new file account.ss.





rhyspj@gwu.edu