[<<Previous Entry]
[^^Up^^]
[Next Entry>>]
[Menu]
[About The Guide]
Class Initialization
When a class is first created (the first time its class function is
called), some initialization may need to take place. For example, if a
class has class variables, those variables may need to be initialized. To
assist with this, if a class defines a class method called initClass, the
method will be invoked automatically when the class is first created. This
allows one-time initialization of class variables. The initClass method
should be declared in the class specification as follows:
CLASS METHOD initClass
and defined after the class specification using the METHOD definition
command. The method will be invoked when its class is first created.
The initClass method should be written with inheritance in mind. A subclass
inherits its own copy of all the class variables defined in its ancestor
classes. When such a subclass is first created, it is necessary to
initialize those inherited variables. If initClass methods have been
properly defined in the ancestor classes, a method definition such as the
following will ensure correct initialization:
METHOD initClass(), ()
// initialize class variables defined in
// this class, but not inherited variables
The first line of this method definition includes an empty second
parameter list, which causes the initClass() method in the superclass to
be invoked, ensuring correct initialization.
Warning
If operations other than initialization of class variables are performed
during the initialization of a class, care must be taken to ensure that
these operations are not repeated unintentionally when subclasses are
created. For example, if a database table is opened during initialization
of a class, it may not be desirable to reopen the table when a subclass is
created. Appropriate conditional code should be included in the initClass
method to prevent this.
Shared class variables - class variables declared with the SHARED
clause of the CLASS VAR command - are particularly prone to this
problem. In most cases, an inherited shared class variable should not be
reinitialized when a subclass is created. To prevent this, code such as
this can be used:
METHOD initClass(), ()
IF ::someVar == NIL
::someVar := <value>
END
RETURN self
Assuming someVar has been declared as a shared class variable, this
initClass method ensures that it will not be initialized more than once,
even when subclasses are created.
See Also:
CLASS METHOD
CLASS VAR
This page created by ng2html v1.05, the Norton guide to HTML conversion utility.
Written by Dave Pearson