What are Constants?

A constant in programming is a named, unchanging value that is assigned a fixed name like a variable. The key difference is that a constant’s value, once declared, does not change during programme execution. It has a value that was assigned at declaration time, is kept in programme memory, and cannot be changed without the risk of a runtime error. Constants are ideal for representing fixed values like the mathematical constant pi (e.g., 3.14) within a program, serving as literal, unchangeable references.

CONSTANTS Statements

The CONSTANTS statement is used to declare named data objects.

Syntax for ABAP Constants:

CONSTANTS constantName TYPE dataType VALUE constantValue.

constantName: specifies a name for the constant.
dataType: represents the data type
constantValue: assigns an initial value to the declared constant name constantName.

Example of ABAP Constants:

CONSTANTS gfgID TYPE I VALUE 1.

In this ABAP example, we declare a constant named ‘gfgID’ of type ‘I’ (integer). It is assigned the value ‘1’, representing a unique identifier for a data record.

Practical Example of ABAP Constants:

A payroll application is one realistic example of using constants in SAP ABAP. Imagine a situation where you must determine an employee’s bonus depending on a number of factors, such as years of service. A constant might be defined as follows:

CONSTANTS cBonusMultiplier TYPE P DECIMALS 2 VALUE '0.05'.

In this instance, the bonus multiplier, denoted by the constant ‘cBonusMultiplier’, is 5% (0.05) of the employee’s pay. Then, you may reliably compute bonuses across the whole application by using this constant in your ABAP code. For instance:

DATA lvEmployeeSalary TYPE P DECIMALS 2.
DATA lvBonus TYPE P DECIMALS 2.

lvEmployeeSalary = 5000.00. " Example employee salary
lvBonus = lvEmployeeSalary * cBonusMultiplier.

WRITE: 'Employee Salary:', lvEmployeeSalary, 'Bonus:', lvBonus.

The bonus computation is kept consistent across the programme by utilising the constant cBonusMultiplier, and if the bonus % ever has to change, you only need to adjust it in one place (the constant declaration) rather than scouring the whole codebase. This lowers the possibility of mistakes and improves the maintainability of your code.

Type of Constants:

There are 3 types of Constants

  1. Elementary constants
  2. Complex constants
  3. Reference constants

1. Elementary Constants

The CONSTANTS statement is used to declare immutable data objects known as elementary constants. Any kind of data, including numeric, character-like, and date/time data, may be stored using them.

For storing quantities that are used repeatedly throughout a programme, such as pi or the speed of light, elementary constants are helpful. Additionally, they may be utilised to construct constants for certain reasons, such as the maximum number of customers allowed in a database table or the default value for a field.

Declaration of an Elementary constant for the value of pi:

CONSTANTS pi TYPE p DECIMALS 10 VALUE '3.1415926536'.

Example of Elementary Constants

DATA: radius TYPE p DECIMALS 10 VALUE 5.0.
DATA: area TYPE p DECIMALS 10.
area = pi ✖ radius ✖ radius.
WRITE: / 'The area of the circle is:', area.

2. Complex Constants

Complex constants in SAP ABAP are collections of elementary constants, such as strings or integers, put together to represent organised data, improving the readability and maintenance of the code.

Declaration of Complex constants

CONSTANTS: BEGIN OF complex-constant-name,

END OF complex-constant-name.

Example of Complex constants

CONSTANTS:
BEGIN OF address,
name TYPE c LENGTH 20 VALUE 'Alfred Milestone',
street TYPE c LENGTH 20 VALUE '14 Avenue',
number TYPE p VALUE 10,
postcode TYPE n LENGTH 5 VALUE 98665,
city TYPE c LENGTH 20 VALUE 'Las Vegas'
END OF address.

Input

WRITE: / address-street.

Output

14 Avenue

3. Reference Constants

A constant that references another data item is known as a reference constant in SAP ABAP. It means that while the value of the data object that a reference constant references can be modified, the value of the reference constant itself cannot.

Any type of data item, including elementary data types, structures, internal tables, and objects, can be represented using reference constants.

Declaration of Reference constants

CONSTANTS:
ref_constant TYPE REF TO object_name VALUE IS INITIAL.

SAP ABAP | Constants & Literals Explained

In the world of SAP ABAP (Advanced Business Application Programming), the use of Constants and Literals is necessary for the effective handling of data. Literals are used to denote specific data types such as numbers, characters, strings, and boolean values.

Constants & Literals in SAP

Similar Reads

What are Literals?

Literals in programming refer to fixed values that are directly represented in the source code of a program. They are used to denote specific data types such as numbers, characters, strings, and boolean values. Literals are constant and do not change during the execution of the program....

What are Constants?

A constant in programming is a named, unchanging value that is assigned a fixed name like a variable. The key difference is that a constant’s value, once declared, does not change during programme execution. It has a value that was assigned at declaration time, is kept in programme memory, and cannot be changed without the risk of a runtime error. Constants are ideal for representing fixed values like the mathematical constant pi (e.g., 3.14) within a program, serving as literal, unchangeable references....

Conclusion

This article goes through the basic concepts of Constants and Literals in SAP ABAP. We looked at Character Literals and Numeric Literals, the basic building blocks for expressing certain values directly in code. Our next goal was to comprehend constants and how to use them via the CONSTANTS declaration. We also looked into real-world use cases to show how Constants and Literals improve readability and flexibility of code. Finally, grasping these fundamental ideas will enable ABAP developers to produce reliable, effective, and maintainable programmes in the SAP environment....