ABAP - Keyword Documentation →  ABAP - Reference →  Calling and leaving program units →  Calling Processing Blocks →  Calling Procedures →  Method Calls →  Static Method Calls →  ... meth1( ... )->meth2( ... )->... - Method Chaining → 

Method Chaining

This example demonstrates method chaining in an operand position.

Source Code

REPORT demo_method_chaining.

CLASS demo DEFINITION.
  PUBLIC SECTION.
    CLASS-METHODS main.
  PRIVATE SECTION.
    METHODS meth  IMPORTING str        TYPE string
                  RETURNING value(ref) TYPE REF TO demo.
    DATA text TYPE string.
ENDCLASS.

CLASS demo IMPLEMENTATION.
  METHOD main.
    DATA oref TYPE REF TO demo.
    CREATE OBJECT oref.
    cl_demo_output=>display(
      oref->meth( `Hello ` )->meth( `world` )->meth( `!` )->text ).
  ENDMETHOD.
  METHOD meth.
    text = text && str.
    ref = me.
  ENDMETHOD.
ENDCLASS.

START-OF-SELECTION.
  demo=>main( ).

Description

In the argument of the method DISPLAY_TEXT statement, a chained attribute access is performed on the attribute text of the class demo. The attribute is changed by three chained method calls.