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.
CONSTRUCTOR [PRIVATE|PACKAGE-PRIVATE|PROTECTED|PACKAGE-PROTECTED|PUBLIC] [STATIC] class-name ( [ parameter [ , parameter ] ... ] ) : constructor-body |
A PRIVATE constructor can be called by the defining class or another instance of the same class. This is done via the THIS-OBJECT statement within another constructor of the class or using the NEW statement or function anywhere within the class. To create the first instance of a class with only a private constructor, you can call a static method in the class which will use NEW to create an instance.
A PACKAGE-PRIVATE constructor can be called from the defining class or any class within its package. This is done via the SUPER statement within another constructor or using the NEW statement or function elsewhere.
A PROTECTED constructor can be called from the defining class or a subclass. This is done via the SUPER statement within another constructor or using the NEW statement or NEW function (classes) elsewhere.
A PACKAGE-PROTECTED constructor can be called from the defining class, from a subclass, or from any class within its package. This is done via the SUPER statement within another constructor or using the NEW statement or function elsewhere.
A PUBLIC constructor can be called from any code (in a class or procedure) that has access to the class instance. This is done via the SUPER statement within another constructor or using the NEW statement or function elsewhere.
The default access mode is PUBLIC.
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.
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.
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.