This is a test message to test the length of the message box.
Login
|
ABAP OO Usage of constants
Created by Software-Heroes

ABAP OO - Usage of constants

2197

Constants are an important component in ABAP development, both for reuse and for defining interfaces. Let's dive into the topic.

Advertising


In this article, we'll look at the definition of constants, their use in development, and how you can easily access the corresponding values.

 

Introduction

Constant values are a central component in development when it comes to using interfaces. For example, you can use them to map different content options and specify the options to the developer using them. This eliminates the need to guess which values should be passed to which parameters, reducing the likelihood of errors. If, for example, the content i.e., the actual value of a constant changes, you can do so at a technical level without causing inconsistency in your colleagues' development.

 

Definition

In this chapter, we'll look at the definition of constants.

 

Individual Values

In principle, we can define each constant individually; this makes sense for individual values, since there is no connection between different values. However, we often find the variant in the standard that groups are formed that have the same beginning. In this example, we define three constants that represent different options for a status.

CONSTANTS option_created  TYPE basic_option VALUE 'C'.
CONSTANTS option_planned  TYPE basic_option VALUE 'P'.
CONSTANTS option_finished TYPE basic_option VALUE 'F'.

 

Groups

An alternative to single groups is defining a structure. Using BEGIN OF and END OF, we can create a group of constants and bundle them under one access. This makes definition, assignment, and use somewhat easier. Another developer will find the related values much easier. To do this, we define the individual values from above again in a structure.

CONSTANTS: 
  BEGIN OF options,
    created  TYPE basic_option VALUE 'C',
    planned  TYPE basic_option VALUE 'P',
    finished TYPE basic_option VALUE 'F',
  END OF options.

 

ENUM

A very special form of constants and structures is enumeration. It allows you to define constant values with a precise check of their contents. The compiler can then determine whether a value is permissible for an interface or not. In this article, we will now address the topic. If you would like more information, you can check out this article on the topic.

With the Simple Types from the Core Data Services area and the Table Entities, enumerations will be used more frequently in the future, as they will work from the database to the UI and there will be no break in the logic, as is currently the case with ENUMs.

 

Usage

This chapter deals with the use of constants during development. We'll cover two different ways of using it.

 

Interface

The first and most commonly used variant is to go through the corresponding interface, where the constants and groups are also defined. We call the interface, call the group, and then the corresponding field or constant of the structure.

IF demo->evaluate_option( zif_bs_demo_oo_constants=>options-created ).
ENDIF.

 

The group of constants gives us a quick overview of the different values that are possible for the OPTION. With the individual values, we must be careful to group them into pseudo-groups and then find them again. With many constants, the overview could suffer, as in the following example.

 

Hint: You can find more information in the Clean ABAP Guidelines.

 

Object

A second option, which we have not found so often so far, is the use of the of the actual object you're currently working with. In this case, we find our options directly after DEMO and can access them. We don't need to know or specify the interface, which makes the notation somewhat shorter.

IF demo->evaluate_option( demo->options-planned ).
ENDIF.

 

Since the constants are defined in the interface, they are also available in the object. This coupling allows us to use them directly.

 

Validation

Unlike ENUMs, however, you must perform the validation for constants yourself. Either you set the values in the process, or you should validate the values passed to the method. Otherwise, errors due to unknown values may occur in the subsequent process. In the worst case, this could cause processing to freeze and post-processing to become impossible.

 

Complete example

We have stored both resources in the corresponding GitHub repository, where you will find the two objects of the article in the ZBS_DEMO_OO_INTERFACE package. You can view the changes from this article via the commit.

 

Conclusion

Constants are an important component when working with interfaces and method interfaces. Even with their definition, you lay the foundation for quick and easy use for you and other developers.


Included topics:
OOABAP OOConstantsENUM
Comments (0)



And further ...

Are you satisfied with the content of the article? We post new content in the ABAP area every Tuesday and Friday and irregularly in all other areas. Take a look at our tools and apps, we provide them free of charge.


ADT - Metadata Wizard [MIA]

Category - ABAP

In this article, we'll take a look at the Metadata Wizard and how it might simplify your life when creating UI annotations in RAP in the future.

01/16/2026

ABAP OO - Injector

Category - ABAP

In the last article, we looked at ABAP OO and the evolution of design patterns, and how we can best use them. Therefore, in this article, we will delve deeper into the details of the injector for testability.

12/23/2025

030: Software-Heroes - My Community

Category - YouTube

Do you want to stay up-to-date with the latest ABAP and SAP knowledge without having to search through every blog individually? My Community brings all community content into a mini-app that you can customize to your liking, so you never miss any news.

12/22/2025

ABAP Tools - IDE Actions (Table)

Category - ABAP

How do you actually create a table and edit it in the IDE Action? Let's look at the input options and how you can ultimately work with the data.

12/09/2025

ABAP Tools - IDE Actions (Side Effects)

Category - ABAP

How can we automatically update information on the UI when something happens with the IDE action? Let's take a closer look at the side effects.

11/18/2025