When you create an instance of another class, you call the constructor for the class using the ABL new keyword. The constructor returns a value, which is the reference to the newly created class instance. You must assign the reference to the class instance to the variable or property whose type is the name of the class.

Note: The class you call may have multiple constructors, each with its own set of parameters. You must make sure that you call the correct constructor by using the expected parameter list for that constructor.

Here is the simplified syntax for calling the default constructor for a class with no parameters:

<defined-name> = new <class-name>().

Syntax Element

Description

defined-name

The name of a previously defined variable or property that will hold the reference to the class instance

class-name

The name of the class

Suppose the application has a Dept class that is used to hold data for employees of a department. A user of the Dept class can call the AddEmployee() method to add an employee to the department. Here is the AddEmployee() method of the Dept class that takes as input all of the data necessary to initialize an Emp instance. It defines a variable, Empl, that will hold the reference to the Emp instance. The next statement creates an instance of the Emp class using the new keyword to call the constructor. In this same statement, the reference to the Emp class instance returned by the constructor is assigned to Emp.

After it is created, the Emp instance can be accessed and initialized.

method public void AddEmployee (  input pEmpNum as integer, 
  input pFirstName as character, 
  input pLastName as character, 
  input pAddress as character, 
  input pPostalCode as character, 
  input pPhones as character extent 3, 
  input pVacationHours as integer, 
  input pJobTitle as character ): 
    define variable Empl as Emp no-undo. 
    Empl = new Emp (). 
     // rest of method to initialize the values for the Employee  
	// instance using the parameters passed into this method
	// then add reference to Employee instance to array of Employees 
  return. 
end method.