Try OpenEdge Now
skip to main content
Object-oriented Programming
Getting Started with Classes, Interfaces, and Objects : Using the CLASS construct : Defining class constructors : CONSTRUCTOR statement
 
CONSTRUCTOR statement
This is the syntax for defining a class constructor:

Syntax

CONSTRUCTOR [ PRIVATE | PROTECTED | PUBLIC | STATIC ]class-name
  ( [parameter[ , parameter]...] ):
  constructor-body
END [ CONSTRUCTOR ] .
Element descriptions for this syntax diagram follow:
[ PRIVATE | PROTECTED | PUBLIC | STATIC ]
Specifies the access mode (PRIVATE, PROTECTED, or PUBLIC) for an instance constructor or STATIC to specify the static constructor. The default access mode for an instance constructor is PUBLIC. PRIVATE instance constructors can be invoked by another constructor defined in the class using the THIS-OBJECT statement. A PRIVATE constructor can also be invoked by an instance of the defining class. PROTECTED instance constructors can be invoked by another constructor defined in the class and by a constructor defined in a subclass of the defining class using the SUPER statement. A PROTECTED constructor can be invoked by an instance of the defining class or a subclass. PUBLIC constructors can be invoked by another constructor defined in the class, by a constructor defined in a subclass of the defining class, and by other classes and procedures that instantiate the defining class using the NEW function, NEW statement, or DYNAMIC-NEW statement.
You can define only one static constructor for a class type. If the class type contains other static members, this defined static constructor (or the default static constructor, if you do not define one) runs once in an ABL session at some point after the first reference to its defining class type, but before any instance constructor of that class type runs. Unless specified otherwise, a reference to a constructor in this manual is assumed to be a reference to an instance constructor. For more information on static constructors, see Usingstatic members of a class.
class-name
The name of the class stripped of any relative path information. This is the class-name portion of the class-type-name defined by the CLASS statement.
[ parameter [ , parameter]...]
Any parameters that you define for an instance constructor. You cannot define parameters for a static constructor. If this instance constructor overloads another instance constructor definition in the class, ABL must be able to disambiguate the two constructors by the number, data types, or modes of their parameter definitions (their signatures). For more information on the syntax of parameter and how to define overloaded constructor signatures, see the Parameter definition syntax reference entry in OpenEdge Development: ABL Reference.
constructor-body
The logic of the constructor, which can be composed of any statements currently allowed within a procedure block along with syntax restricted to methods. In an instance constructor only, additional statements allowed include the SUPER statement, which invokes an instance constructor defined in the immediate super class, and the THIS-OBJECT statement, which invokes another instance constructor defined in the current class. In a static constructor, you can only access other static members defined within the current class hierarchy in addition to local method data; instance members defined within the current class hierarchy are inaccessible. However, in an instance constructor, constructor-body statements can access static members as well as other instance members. In general, the constructor logic is typically used to initialize accessible data members and properties of the class.
For instance constructors only, if the constructor is for a subclass and every constructor in the immediate super class takes parameters (there is no default), the first executable statement in the constructor must be an explicit call to a constructor of the immediate super class using the SUPER statement. If there is a super class constructor that does not take parameters (serving as a defined default), an explicit call from the subclass is optional. If you do not explicitly invoke a super class constructor, the AVM automatically invokes the super class's default constructor before executing any statements in the subclass constructor. If you invoke a RETURN ERROR or an UNDO, THROW that returns ERROR from a constructor, the AVM automatically calls the class destructor, which halts class instantiation and deletes any classes that have completed construction in the class hierarchy. For more information on how the AVM handles super class and subclass constructors in a class hierarchy, see Constructingan object. For more information on coding the logic for a static constructor, see Definingstatic members.
Adding a constructor to the acme.myObjs.CustObj sample class definition results in the following code:
USING acme.myObjs.*.
USING acme.myObjs.Common.*.

CLASS acme.myObjs.CustObj INHERITS CommonObj:

  DEFINE PUBLIC VARIABLE iNumCusts AS INTEGER NO-UNDO.

  DEFINE PROTECTED TEMP-TABLE ttCustomer NO-UNDO
    FIELD RecNum AS INTEGER
    FIELD CustNum LIKE Customer.CustNum
    FIELD Name LIKE Customer.Name
    FIELD State AS CHARACTER.

  DEFINE PRIVATE VARIABLE rCreditObj AS CLASS CreditObj NO-UNDO.
  DEFINE PRIVATE VARIABLE rMsg       AS CLASS MsgObj    NO-UNDO.

  CONSTRUCTOR PUBLIC CustObj ( ):
    rCreditObj = NEW CreditObj ( ).
    iNumCusts  = 0.

    /* Fill temp table and get row count */
    FOR EACH Customer WHERE Customer.CreditLimit > 50000:
      CREATE ttCustomer.
      ASSIGN
        iNumCusts = iNumCusts + 1
       ttCustomer.RecNum  = iNumCusts
        ttCustomer.CustNum = Customer.CustNum
        ttCustomer.Name  = Customer.Name
        ttCustomer.State  = Customer.State.
    END.
    rMsg = MessageHandler("acme.myObjs.CustObj").
  END CONSTRUCTOR.  

METHOD PUBLIC CHARACTER GetCustomerName (INPUT piRecNum AS INTEGER):
    FIND ttCustomer WHERE ttCustomer.RecNum = piRecNum NO-ERROR.
    IF AVAILABLE ttCustomer THEN
      RETURN ttCustomer.Name.
    ELSE DO:
      rMsg:Alert("Customer number" + STRING(ttCustomer.RecNum) +
                 "does not exist").
      RETURN ?.
    END.
  END METHOD.

END CLASS.