Show TOC

Reusing Existing ConstantsLocate this document in the navigation structure

Prerequisites

An existing constant is already defined for the same literal in a certain ABAP class.

Context

You can reuse a member or local constant that has already been generated.

Note that all occurrences of the same literal remain unchanged.

Example

The literal '200' is to be replaced with an existing constant:

CLASS cl_car DEFINITION. 
 PUBLIC SECTION . 
  METHODS: drive 
   IMPORTING 
    i_speed TYPE i. 
 PRIVATE SECTION . 
  CONSTANTS max_speed TYPE i VALUE 220. 
  DATA: current_speed TYPE i. 
ENDCLASS. 
 
CLASS cl_car IMPLEMENTATION. 
 METHOD drive. 
  IF i_speed > 220. 
   current_speed = 220. 
  ELSE. 
   current_speed = i_speed. 
  ENDIF. 
 ENDMETHOD. 
ENDCLASS.

Procedure

  1. In the source code editor, select the literal that is to be replaced by an existing constant.
  2. In the context menu, choose Quick Fix or use the shortcut (Ctrl 1).
  3. In the Quick Fix dialog box, double-click Use local constant or Use member constant.

Results

In the source code of the ABAP class, all occurrences of the literal are replaced with the name of the existing constant.

Example

ABAP class after reusing an existing constant:

CLASS cl_car DEFINITION. 
 PUBLIC SECTION . 
  METHODS: drive 
   IMPORTING 
    i_speed TYPE i. 
 PRIVATE SECTION . 
  CONSTANTS max_speed TYPE i VALUE 220. 
  DATA: current_speed TYPE i. 
ENDCLASS. 
 
CLASS cl_car IMPLEMENTATION. 
 METHOD drive. 
  IF i_speed > max_speed. 
   current_speed = max_speed. 
  ELSE. 
   current_speed = i_speed. 
  ENDIF. 
 ENDMETHOD. 
ENDCLASS.