Contents | Prev | Next | Index


Static Methods

Methods declared in a class type are by default static. When a static method is called, the declared compile-time type of the class or object used in the method call determines which method implementation to activate.

In the following example, the Draw methods are static:

TYPE
  TFigure = CLASS
    PROCEDURE Draw;
    :
  END;
  TRectangle = CLASS( TFigure )
    PROCEDURE Draw;
    :
  END;

The following section of code illustrates the effect of calling a static method. Even though in the second call to Figure.Draw the Figure variable references an object of class TRectangle, the call invokes the implementation of TFigure.Draw because the declared type of the figure variable is TFigure.

VAR
  Figure : TFigure;
  Rectangle : TRectangle;
BEGIN
  Figure := NEW( TFigure );
  Figure.Draw; {invokes TFigure.Draw}
  DISPOSE( Figure );
  Figure := NEW( TRectangle );
  Figure.Draw; {invokes TFigure.Draw}
  DISPOSE( Figure );
  Rectangle := NEW( TRectangle );
  Rectangle.Draw; {invokes TRectangle.Draw}
  DISPOSE( Rectangle );
END;

Notice that Pascal-static methods are NOT the same as Java-static methods. A Pascal static method can operate on instance members of the class for which it is invoked, while a Java-static method is only a class method, which is unable to access any instance members of the class. Canterbury Pascal maps a Pascal-static method to a Java-virtual method, but uses a unique method name of the form class-name_method-name. This way it makes sure that a method invocation will always activate the method of the compile-time declared type of the object variable.


Contents | Prev | Next | Index

Canterbury Pascal for JVM  (Last documentation update Sep 02, 2004)
Copyright © 1999-2004 J.Neuhoff - mhccorp.com  . All rights reserved.
Please send any comments or corrections to neuhoff@mhccorp.com