Show TOC

Generating Getters and SettersLocate this document in the navigation structure

You can generate the getter and/or setter method stubs from an attribute in the definition of an ABAP class in the implementation. In addition, the corresponding importing and/or returning parameter(s) are set.

Prerequisites

This functionality is provided since SAP NetWeaver 7.5 SP00.

Context

Example

Before Execution After Execution

To copy the source code example, click here

In the local cl_car class of an ABAP program, the speed attribute is defined. For the latter, the getter and setter methods and the corresponding returning and importing parameters need to be generated. The reason for this is because their value should be accessed and encapsulated.

In the definition part of the cl_car class, the get_speed and set_speed methods are added. For the get_speed method, the r_speed returning parameter is set and for the set_speed method the i_speed importing parameter is set.

In the implementation part, the corresponding method stubs and their returing and importing parameters are generated on the basis of the selected attribute.

Procedure

  1. In the definition part, select the variable for which you want to create the getter and setter methods.
  2. Get the quick assist proposals by choosing Quick Fix from the context menu (shortcut Ctrl 1) or by using the Quick Assist view.
  3. Apply one of the following options.
    • Generate Getter and Setter for speed to add the get_speed and set_speed method stubs as well as its i_speed importing and r_speed returning parameters. In addition, the corresponding definitions are also added.
    • Generate Getter for speed to add the get_speed method stub where the r_speed returning parameter is set. The corresponding definitions are also added.
    • Generate Setter for speed to add the set_speed method stub where the i_speed importing parameter is set. The corresponding definitions are also added.