Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
SAP ABAP: Using Elementary data type and reference type with keyword VALUE
In SAP ABAP, the VALUE keyword is used to assign default values to variables, constants, and parameters at the time of declaration. When combined with elementary data types and reference types, the VALUE keyword allows you to initialize data objects with predefined values, ensuring they hold meaningful content from the moment they are created.
Understanding how VALUE works with different data types is essential for writing clean, predictable ABAP programs. This article explains how to use the VALUE keyword with elementary data types (such as integers, strings, and dates) and reference types (such as object references and data references).
Elementary Data Types in ABAP
Elementary data types are the basic building blocks of data in ABAP. They hold single, scalar values and are not composed of other types. The most commonly used elementary data types include −
- I − Integer (whole numbers)
- F − Floating point numbers
- STRING − Variable-length character string
- C − Fixed-length character
- N − Numeric text
- D − Date (format YYYYMMDD)
- T − Time (format HHMMSS)
- P − Packed number (for decimal arithmetic)
- XSTRING − Variable-length byte string
Each of these types has its own initial (default) value. For instance, an integer is initialized to 0, a string to an empty string, and a date to 00000000. The VALUE keyword lets you override these defaults.
Using VALUE with Elementary Data Types
The VALUE keyword is placed in a DATA statement to set an initial value for a variable at declaration time. The syntax is straightforward −
Syntax
DATA variable_name TYPE data_type VALUE initial_value.
The value you assign must be compatible with the declared type. For example, you cannot assign a text string as the value of an integer variable.
Example: VALUE with Common Elementary Types
In the following example, we declare variables of various elementary types and assign default values using VALUE ?
REPORT z_value_elementary.
* Integer with VALUE
DATA lv_count TYPE i VALUE 42.
* String with VALUE
DATA lv_name TYPE string VALUE 'TutorialsPoint'.
* Fixed-length character with VALUE
DATA lv_code(5) TYPE c VALUE 'ABCDE'.
* Numeric text with VALUE
DATA lv_num(10) TYPE n VALUE '0000012345'.
* Date with VALUE
DATA lv_date TYPE d VALUE '20250315'.
* Time with VALUE
DATA lv_time TYPE t VALUE '143000'.
* Packed number with VALUE
DATA lv_price TYPE p LENGTH 8 DECIMALS 2 VALUE '1299.99'.
* Floating point with VALUE
DATA lv_ratio TYPE f VALUE '3.14'.
WRITE: / 'Count: ', lv_count,
/ 'Name: ', lv_name,
/ 'Code: ', lv_code,
/ 'Num: ', lv_num,
/ 'Date: ', lv_date,
/ 'Time: ', lv_time,
/ 'Price: ', lv_price,
/ 'Ratio: ', lv_ratio.
Count: 42 Name: TutorialsPoint Code: ABCDE Num: 0000012345 Date: 20250315 Time: 143000 Price: 1,299.99 Ratio: 3.1400000000000001E+00
Each variable is initialized with the specified value immediately upon creation. Without the VALUE addition, each variable would contain its type-specific initial value (zero for numbers, spaces for characters, etc.).
Using VALUE with Constants
The CONSTANTS statement in ABAP requires the VALUE keyword. Unlike variables declared with DATA, constants must always be initialized and cannot be changed during program execution.
Syntax
CONSTANTS constant_name TYPE data_type VALUE initial_value.
Example: Declaring Constants with VALUE
REPORT z_value_constants.
* Constants must always use VALUE
CONSTANTS gc_pi TYPE p LENGTH 8 DECIMALS 5 VALUE '3.14159'.
CONSTANTS gc_company TYPE string VALUE 'TutorialsPoint'.
CONSTANTS gc_max_items TYPE i VALUE 100.
CONSTANTS gc_tax_rate TYPE p LENGTH 5 DECIMALS 2 VALUE '18.00'.
WRITE: / 'PI: ', gc_pi,
/ 'Company: ', gc_company,
/ 'Max Items: ', gc_max_items,
/ 'Tax Rate: ', gc_tax_rate.
PI: 3.14159 Company: TutorialsPoint Max Items: 100 Tax Rate: 18.00
Attempting to modify a constant after declaration results in a syntax error. This makes constants ideal for storing fixed configuration values like tax rates, mathematical constants, or application-wide limits.
The IS INITIAL Check and VALUE
When you use VALUE, the variable is no longer in its initial state (unless the assigned value happens to match the type's default initial value). You can check whether a variable holds its initial value using the IS INITIAL predicate ?
Example: IS INITIAL with VALUE
REPORT z_value_is_initial. DATA lv_with_value TYPE i VALUE 10. DATA lv_without_value TYPE i. IF lv_with_value IS INITIAL. WRITE: / 'lv_with_value is initial'. ELSE. WRITE: / 'lv_with_value is NOT initial, value:', lv_with_value. ENDIF. IF lv_without_value IS INITIAL. WRITE: / 'lv_without_value is initial, value:', lv_without_value. ELSE. WRITE: / 'lv_without_value is NOT initial'. ENDIF.
lv_with_value is NOT initial, value: 10 lv_without_value is initial, value: 0
Reference Types in ABAP
Reference types hold pointers (references) to other data objects or class instances rather than holding the data directly. ABAP supports two categories of reference types −
-
Data references − Declared with
TYPE REF TO dataorTYPE REF TOa specific data type. They point to data objects in memory. -
Object references − Declared with
TYPE REF TOa class or interface name. They point to instances of ABAP Objects classes.
By default, all reference variables are initialized to a null reference (they point to nothing). The VALUE keyword has a specific and limited role with reference types.
Using VALUE with Reference Types
For reference type variables, the only valid value you can assign with the VALUE keyword is INITIAL. You cannot point a reference to a specific object or data area at declaration time because object creation and memory allocation happen at runtime using CREATE OBJECT or CREATE DATA.
Syntax
DATA ref_variable TYPE REF TO some_type VALUE IS INITIAL.
Since references are already initial by default, using VALUE IS INITIAL is technically redundant. However, it serves as explicit documentation of the programmer's intent, making the code more readable.
Example: Data References with VALUE
In the following example, we declare data references, use VALUE IS INITIAL, and then assign them to data objects at runtime ?
REPORT z_value_data_ref.
* Declare data references with VALUE IS INITIAL
DATA lr_int TYPE REF TO i VALUE IS INITIAL.
DATA lr_string TYPE REF TO string VALUE IS INITIAL.
* Check initial state
IF lr_int IS INITIAL.
WRITE: / 'lr_int is initial (null reference)'.
ENDIF.
* Create data objects and assign references
CREATE DATA lr_int.
CREATE DATA lr_string.
* Assign values via dereferencing
lr_int->* = 500.
lr_string->* = 'Hello from TutorialsPoint'.
WRITE: / 'Integer value: ', lr_int->*,
/ 'String value: ', lr_string->*.
* Check after assignment
IF lr_int IS NOT INITIAL.
WRITE: / 'lr_int now points to a data object'.
ENDIF.
lr_int is initial (null reference) Integer value: 500 String value: Hello from TutorialsPoint lr_int now points to a data object
After CREATE DATA, the reference variable points to a newly allocated memory area. The VALUE IS INITIAL clause simply confirms the starting state before any runtime assignment.
Example: Object References with VALUE
Object references follow the same rule. You can only use VALUE IS INITIAL at declaration time ?
REPORT z_value_obj_ref.
* Define a simple class
CLASS lcl_calculator DEFINITION.
PUBLIC SECTION.
METHODS add
IMPORTING iv_a TYPE i
iv_b TYPE i
RETURNING VALUE(rv_result) TYPE i.
ENDCLASS.
CLASS lcl_calculator IMPLEMENTATION.
METHOD add.
rv_result = iv_a + iv_b.
ENDMETHOD.
ENDCLASS.
* Declare object reference with VALUE IS INITIAL
DATA lo_calc TYPE REF TO lcl_calculator VALUE IS INITIAL.
IF lo_calc IS INITIAL.
WRITE: / 'lo_calc is initial before CREATE OBJECT'.
ENDIF.
* Instantiate the object
CREATE OBJECT lo_calc.
* Use the object
DATA(lv_sum) = lo_calc->add( iv_a = 25 iv_b = 17 ).
WRITE: / 'Sum: ', lv_sum.
IF lo_calc IS NOT INITIAL.
WRITE: / 'lo_calc now references an object instance'.
ENDIF.
lo_calc is initial before CREATE OBJECT Sum: 42 lo_calc now references an object instance
VALUE in Inline Declarations
Modern ABAP (from ABAP 7.40 onwards) supports inline declarations using DATA(). With inline declarations, the value is determined by the right-hand side of the assignment rather than the VALUE keyword. However, the VALUE constructor operator can be used to create typed initial values inline ?
Example: VALUE Constructor Operator
REPORT z_value_inline.
* VALUE constructor for elementary types
DATA(lv_number) = VALUE i( ). " Initialized to 0
DATA(lv_text) = VALUE string( ). " Initialized to empty string
WRITE: / 'Number:', lv_number,
/ 'Text: ', lv_text.
* Direct inline assignment (value comes from expression)
DATA(lv_total) = 100 + 200.
WRITE: / 'Total: ', lv_total.
Number: 0 Text: Total: 300
The VALUE constructor operator is different from the VALUE addition in DATA statements. The constructor creates a value of a specific type and is especially powerful when working with structures and internal tables.
Comparison: VALUE with Elementary Types vs Reference Types
| Aspect | Elementary Types | Reference Types |
|---|---|---|
| Allowed values with VALUE | Any compatible literal or constant | Only IS INITIAL
|
| Default without VALUE | Type-specific initial value (0, spaces, etc.) | Null reference (initial) |
| VALUE is mandatory? | No (optional for DATA, mandatory for CONSTANTS) | No (always optional) |
| Runtime assignment | Direct with = operator |
Via CREATE DATA or CREATE OBJECT
|
| Typical use of VALUE | Set meaningful defaults, define constants | Document intent, explicit initialization |
Conclusion
The VALUE keyword in SAP ABAP serves an important role in initializing data objects at declaration time. For elementary data types, it allows you to set any compatible literal value, making your variables ready to use immediately. For constants, the VALUE addition is mandatory. For reference types, the only permitted value is IS INITIAL, since actual object or data creation must happen at runtime. Understanding these distinctions helps you write clearer, more intentional ABAP code and avoid unexpected initial values in your programs.
