Contents | Prev | Next | Index


Destructors

A destructor is a special method that controls the destructions of objects. A class can have zero or more destructors for objects of the class type. Each is specified as a component of the class in the same way as a procedure method, except that the reserved word DESTRUCTOR begins each declaration instead of PROCEDURE. The following grammar shows the destructor procedure heading, with Java-specific language extensions marked in red:

procedure_heading -> destructor proc_id
                  -> destructor proc_id formal_parameters
destructor        -> modifier DESTRUCTOR
modifier          -> java_modifiers
                  -> <empty>
proc_id           -> ident
                  -> ident . member_id
                  -> ident [ <string> ]
                  -> ident . member_id [ <string> ]
                  -> ident java_name
                  -> ident . member_id java_name

Like other methods, destructors can be inherited. A destructor cannot be a class method because it requires access to the instance members of the object for which it was invoked. Destructors are always mapped to regular Java-methods in the same way as it is done for static methods or virtual methods. Example:

TYPE
  TRectangle = CLASS( TFigure )
    lenght : INTEGER;
    width : INTEGER;
    CONSTRUCTOR Create();
      OVERRIDE;
    CONSTRUCTOR Init( length, width : INTEGER );
      VIRTUAL;
    DESTRUCTOR;
      VIRTUAL;
  END;

A destructor method can be invoked via standard procedure DISPOSE where the whole destructor call becomes the second actual parameter. It can also be invoked like a normal method call. In the latter the object itself is not destroyed. Notice, that Canterbury Pascal makes use of Java's automatic garbage collection for unused objects, hence normally there is no need for using destructors. This is different from Borland Object Pascal where objects are to be destroyed explicitly.


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