CONSTRUCTOR statement

Defines a constructor for a class. A constructor is a special type of method that ABL invokes to initialize data for a new object of a class that is instantiated using the NEW function (classes), NEW statement, or DYNAMIC-NEW statement, or to initialize static members of a class.

Note: This statement is applicable only when used in a class definition (.cls) file.

Syntax

CONSTRUCTOR [ PRIVATE | PROTECTED | PUBLIC | STATIC ] class-name 
  ( [ parameter [ , parameter ] ... ] ) :

  constructor-body
[ PRIVATE | PROTECTED | PUBLIC | STATIC ]
Specifies the access mode for the constructor.

PRIVATE specifies an instance constructor that you can call explicitly within another constructor of the defining class (using the THIS-OBJECT statement) during class instantiation, or that a static method or static constructor of the class can invoke by executing the NEW function (classes) in order to allow the class to instantiate itself. An instance can directly call a private constructor of its own class.

PROTECTED specifies an instance constructor that you can call explicitly within a constructor of an immediately inheriting subclass (using the SUPER statement) during class instantiation. An instance can call a protected constructor as long as the constructor is defined in the same class as the instance or a superclass of the instance.

PUBLIC specifies an instance constructor that you can call explicitly from within another constructor in the defining class during class instantiation, that you can call explicitly from within a constructor of an immediately inheriting subclass during class instantiation, and that you can call implicitly from any class or procedure when you instantiate the class.

STATIC specifies a static constructor that executes exactly once in an ABL session the first time you reference a class type that defines this constructor in its class hierarchy. You cannot invoke a static constructor in any other way or at any other time. You can define only one static constructor for a given class. If you do not define a static constructor, ABL defines a default static constructor to initialize the static members of a class.

The default access mode is PUBLIC.

class-name
The name of the class this method constructs. This name must match the class name portion of the type name for the class (that is, the name of the class definition file excluding the .cls extension and any package path information).
( [ parameter [ , parameter]...] )
Optionally specifies one or more parameters of the constructor. Any instance constructor defined without a parameter list is the default instance constructor for the defining class. The parameter list for a defined static constructor must be empty.

If this instance constructor is one of several overloaded constructors defined for a class, the parameter list must be unique among all the other constructors. This uniqueness can be established using a different combination of number, data types, or modes for the parameters. For information on the parameter definition syntax and establishing uniqueness for overloaded constructors, see the Parameter definition syntax reference entry. Note that any defined static constructor does not participate in constructor overloading with instance constructors.

constructor-body
The body of the constructor definition. Define the constructor body using the following syntax:
constructor-logic
. . . 
END [ CONSTRUCTOR ].
constructor-logic
The logic of the constructor, which can contain any ABL statements currently allowed within a PROCEDURE block including class-related statements. These statements typically contain logic to initialize the data members and properties in the class.

Each logic statement must end with a period.

If you are defining an instance constructor, regardless of other statements in the constructor, the first action of the constructor must be a call to another instance constructor in the defining class or in the immediate super class. ABL can call a default super class constructor implicitly, or you can call a super class constructor or another overloaded constructor in the defining class explicitly as the first statement of a constructor. You cannot explicitly call another constructor in any other statement of a constructor, and you cannot call any defined static constructor.

If there is no constructor instance defined in the immediate super class and you do not explicitly invoke a constructor, ABL always implicitly invokes the built-in default super class constructor (without parameters) as the first action. If there is an instance constructor defined in the super class that does not take parameters, you also do not need to explicitly invoke an instance constructor. ABL implicitly invokes this user-defined default super class constructor. You only need to explicitly invoke another instance constructor when the super class has constructors defined for it and all of these constructors take parameters.

When you invoke an instance constructor that takes parameters, again, you must invoke that constructor as the first executable statement in the invoking constructor. If you want to invoke a super class instance constructor, you must invoke the SUPER statement with parameters that match the parameters of the super class constructor with respect to number, data type, and mode.

If you want to invoke an overloaded instance constructor of the defining class, you must invoke the THIS-OBJECT statement as the first statement, with parameters that match the parameters of the overloaded constructor with respect to number, data type, and mode. If you invoke an overloaded constructor, and that overloaded constructor does not invoke another overloaded constructor, it must invoke a super class constructor, either implicitly (the default) or explicitly as its first statement. So, in any chain of explicit calls from one overloaded constructor to another, the last overloaded constructor in the chain must invoke a super class constructor.

If you are defining a static constructor, you cannot access any instance members of a class (including the defining class), nor can you use the SUPER and THIS-OBJECT statements. From a static constructor, you can access only other static members of a class and the local variables or other local data elements of the constructor.

END [ CONSTRUCTOR ]
Specifies the end of the constructor body definition. You must end the constructor body definition with the END statement.

Examples

The following example shows the definition of an instance constructor:

CONSTRUCTOR PUBLIC CustObj( ):

  m_NumCusts = 0.

  /* Fill a temp table and get the row count */
  FOR EACH Customer NO-LOCK:
    CREATE ttCust.
    ASSIGN
      ttCust.CustNum = Customer.CustNum
      ttCust.Name    = Customer.Name
      m_NumCusts     = m_NumCusts + 1.
  END.

END CONSTRUCTOR.

For more examples of constructor definitions, including a static constructor and a constructor for an abstract class, see the descriptions of r-CustObj.cls, r-CustObjStatic.cls, and r-CustObjAbstract.cls in the CLASS statement reference entry.

Notes

See also

Assignment (=) statement, CLASS statement, DESTRUCTOR statement, DYNAMIC-NEW statement, FUNCTION statement, NEW function (classes), NEW statement, Parameter definition syntax, SUPER statement, THIS-OBJECT statement