Show TOC

Generating Constructor MethodsLocate this document in the navigation structure

Context

From the class name in the definition of an ABAP class, you can create a public instance method called constructor.

If the class has attributes, a dialog is opened where you can select the attributes that should be instantiated by the constructor. Afterwards, the constructor is created with the selected parameters.

Example

The car class has the name attribute:

CLASS cl_car_constructor_generator DEFINITION PUBLIC CREATE PUBLIC . 
 PUBLIC SECTION. 
ENDCLASS. 
 
CLASS cl_car_constructor_generator IMPLEMENTATION. 
ENDCLASS.

Procedure

  1. In the definition part, select the name of the class.
  2. In the context menu, choose Quick Fix or use the shortcut (Ctrl 1).
  3. In the Quick Fix dialog box, double-click Generate constructor.
  4. A dialog is opened were you can select the attributes which should initialized by the constructor. Select the desired attributes and confirm with OK.

Results

In the definition, the constructor method is added. For each selected attribute, the corresponding parameter is created.

In the implementation part, each parameter is assigned to the corresponding attribute.

Example

ABAP class after generating

CLASS cl_car_constructor_generator DEFINITION PUBLIC CREATE PUBLIC . 
 PUBLIC SECTION. 
  CLASS-METHODS class_constructor. 
ENDCLASS. 
 
CLASS cl_car_constructor_generator IMPLEMENTATION. 
 METHOD class_constructor. 
 ENDMETHOD.
ENDCLASS.