Show TOC

Converting Locals to Class MembersLocate this document in the navigation structure

Context

In SAP NetWeaver 7.4 SP05 or higher, you can convert definitions of a local variable, local constant, or local type and paste them into the private section of the current class. Therefore, you can make certain local elements of individual methods available for all methods within an ABAP class.

Note In the following table, you can see the local elements that can be converted to the corresponding member:
Local Elements Members
Local variables Attributes
Local constants Member constants
Local types Member types
Field symbols Cannot be converted to members

Example

The local variable current_speed, the local constant max_speed VALUE 100, and the local type zy_speed TYPE i are to be declared in the private section:

CLASS cl_local_variable_expression IMPLEMENTATION. 
 METHOD check_area. 
  DATA: length, width, result TYPE i. 
  IF ( length * width > 100 ). 
   result = abap_true. 
  ENDIF. 
 ENDMETHOD. 
ENDCLASS.

Procedure

  1. In the source code editor, navigate to the method signature and position the cursor on the definition of the local variable or type.
  2. In the context menu, choose Quick Fix or use the shortcut (Ctrl 1).
  3. In the Quick Fix dialog box, double-click Convert 'local variable' to attribute, Convert 'local constant' to constant, or Convert 'local type' to class type.

Results

The source code editor inserts the declaration of the local variable current_speed, the local constant max_speed VALUE 100, and the local type zy_speed TYPE i in the private section of the class definition.

Example

ABAP class after conversion:

CLASS cl_variables_class_members DEFINITION PUBLIC FINAL CREATE PUBLIC . 
 PRIVATE SECTION. 
  TYPES zy_speed TYPE i. 
  CONSTANTS max_speed VALUE 100. 

  DATA: current_speed TYPE i. 
  METHODS check_speed 
   RETURNING 
    VALUE(result) TYPE i. 
ENDCLASS. 
 
CLASS cl_variables_class_members IMPLEMENTATION. 
 METHOD check_speed. 
  IF ( current_speed > max_speed ). 
   result = abap_false. 
  ENDIF. 
 ENDMETHOD. 
ENDCLASS.