Using constants in ABAP OO method

When working with constants in ABAP Object-Oriented methods, it's important to organize your code properly. Your INCLUDE should only contain definitions of constants nothing else and then the answer is straightforward.

An INCLUDE in ABAP is a way to modularize code by storing reusable components like constants, type definitions, or data declarations in separate files that can be included in multiple programs or classes.

Steps to Include Constants in ABAP OO Method

To expose constants from an INCLUDE in your ABAP class implementation, follow these steps ?

1. Select Goto ? Class - whichever local definition and then add an INCLUDE statement. This will expose all the constants "INCLUDE" in your implementation.

Example

Here's how to properly structure your constants INCLUDE and use it in your class ?

*--- Constants Include (ZCL_CONSTANTS) ---
CONSTANTS: c_status_active   TYPE char1 VALUE 'A',
           c_status_inactive TYPE char1 VALUE 'I',
           c_max_records     TYPE i VALUE 100.

In your class implementation, include the constants ?

CLASS zcl_my_class DEFINITION.
  PRIVATE SECTION.
    INCLUDE zcl_constants.
    
    METHODS: process_data
      IMPORTING iv_status TYPE char1.
ENDCLASS.

CLASS zcl_my_class IMPLEMENTATION.
  METHOD process_data.
    IF iv_status = c_status_active.
      " Process active records
      WRITE: 'Processing active records, max:', c_max_records.
    ELSEIF iv_status = c_status_inactive.
      " Process inactive records  
      WRITE: 'Processing inactive records'.
    ENDIF.
  ENDMETHOD.
ENDCLASS.

Benefits

This approach provides several advantages ?

? Reusability - Constants can be shared across multiple classes
? Maintainability - Single point of change for constant values
? Organization - Keeps class definitions clean and focused

Conclusion

Using INCLUDE statements for constants in ABAP OO methods promotes code reusability and maintainability by centralizing constant definitions in separate, reusable components.

Updated on: 2026-03-13T18:07:26+05:30

443 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements