Modern ABAP Syntax (7.40+)
ABAP 7.40, released with SAP NetWeaver 7.40 SP05 in 2013, introduced a wave of new language features that dramatically modernized the language. Inline declarations, constructor operators, and expression-oriented syntax reduce boilerplate and make code far more readable. If you are working with S/4HANA or ABAP Cloud, mastering these features is essential.
Inline DATA Declarations
Before ABAP 7.40, every variable had to be declared in a separate DATA block
at the top of the program or method. Inline declarations with DATA(...) let you
declare variables exactly where they are first used, and the type is inferred automatically from context.
This eliminates a major source of verbosity in ABAP programs.
" Old style: declare up front, use later
DATA lv_result TYPE string.
lv_result = 'Hello'.
WRITE: / lv_result.
" Modern style: declare inline at point of use
DATA(lv_modern) = 'Hello'. " type inferred as string
WRITE: / lv_modern.
" Inline in LOOP (very common pattern)
TYPES: BEGIN OF ty_item,
id TYPE i,
name TYPE string,
END OF ty_item.
DATA lt_items TYPE TABLE OF ty_item.
lt_items = VALUE #( ( id = 1 name = 'Alpha' ) ( id = 2 name = 'Beta' ) ).
LOOP AT lt_items INTO DATA(ls_item).
WRITE: / ls_item-id, ls_item-name.
ENDLOOP.
Constructor Operators: NEW and VALUE
NEW creates a new object instance inline, replacing the old
CREATE OBJECT statement. VALUE creates
and initializes structured data and internal tables in a single expression. Both operators use
# as a type placeholder when the type can be inferred from context,
keeping the code concise.
" NEW: create object inline
" Old style:
" DATA lo_obj TYPE REF TO cl_some_class.
" CREATE OBJECT lo_obj.
" Modern style:
DATA(lo_obj) = NEW cl_abap_math( ).
" VALUE: initialize a structure inline
TYPES: BEGIN OF ty_address,
street TYPE string,
city TYPE string,
zip TYPE string,
END OF ty_address.
" Old style required multiple assignments
" Modern style: all in one expression
DATA(ls_addr) = VALUE ty_address(
street = '123 Main St'
city = 'Walldorf'
zip = '69190'
).
WRITE: / ls_addr-city.
" VALUE for internal tables (compact initialization)
DATA(lt_numbers) = VALUE int4_table( ( 10 ) ( 20 ) ( 30 ) ).
LOOP AT lt_numbers INTO DATA(lv_num).
WRITE: / lv_num.
ENDLOOP.
Method Chaining
Modern ABAP supports fluent method chaining: calling methods directly on the return value of another method without storing intermediate results. This style is common with string utility classes and builder patterns. Combined with inline declarations, it eliminates temporary variables that exist only to pass values between calls.
" Old style: temporary variable for each step
DATA lo_regex TYPE REF TO cl_abap_regex.
DATA lo_matcher TYPE REF TO cl_abap_matcher.
CREATE OBJECT lo_regex EXPORTING pattern = '\d+'.
lo_matcher = lo_regex->create_matcher( text = 'abc123def' ).
" Modern style: chain the calls
DATA(lo_matcher2) = cl_abap_regex=>create( pattern = '\d+' )
->create_matcher( text = 'abc123def' ).
" String expressions with method calls inline
DATA lv_text TYPE string VALUE ' hello world '.
DATA(lv_trimmed) = condense( lv_text ).
WRITE: / |'{ lv_trimmed }'|.
Why Modern Syntax Matters for S/4HANA Migration
ABAP Cloud (the development model for S/4HANA and BTP) enforces the use of modern syntax through the
clean core paradigm. Many legacy statements like MOVE,
COMPUTE, and obsolete form routines are restricted or disallowed.
Starting to use modern syntax now — even on older systems — makes future migration smoother and produces
code that abaplint's modernization rules will not flag as problematic.
" Legacy statements (avoid in new code)
MOVE lv_a TO lv_b. " use: lv_b = lv_a.
COMPUTE lv_sum = lv_a + lv_b. " use: lv_sum = lv_a + lv_b.
ADD 1 TO lv_counter. " use: lv_counter += 1.
MULTIPLY lv_val BY 2. " use: lv_val *= 2.
" Modern equivalents (clean core compliant)
DATA lv_a TYPE i VALUE 5.
DATA lv_b TYPE i.
lv_b = lv_a.
DATA lv_sum TYPE i.
lv_sum = lv_a + lv_b.
DATA lv_counter TYPE i.
lv_counter += 1.
WRITE: / lv_counter.
Full Example
This complete example showcases inline declarations, VALUE initialization, and modern loop patterns together. Paste it into ABAP Dojo and click Run to compare the output.
REPORT zmodern_syntax.
TYPES: BEGIN OF ty_product,
id TYPE i,
name TYPE string,
price TYPE p LENGTH 6 DECIMALS 2,
END OF ty_product.
" VALUE: compact table initialization
DATA(lt_products) = VALUE TABLE OF ty_product(
( id = 1 name = 'Widget A' price = '9.99' )
( id = 2 name = 'Widget B' price = '14.99' )
( id = 3 name = 'Widget C' price = '4.50' )
).
WRITE: / '=== Product Catalog ==='.
" Inline DATA in LOOP
LOOP AT lt_products INTO DATA(ls_product).
WRITE: / ls_product-id, ls_product-name, ls_product-price.
ENDLOOP.
" LINES() with inline result
WRITE: / '---'.
WRITE: / 'Total products:', LINES( lt_products ).
" Modern arithmetic
DATA(lv_total) = REDUCE p LENGTH 6 DECIMALS 2
( INIT sum = CONV p( 0 )
FOR row IN lt_products
NEXT sum = sum + row-price ).
WRITE: / 'Total price:', lv_total.