Skip to content

ABAP Keyword Documentation →  ABAP − Reference →  Calling and leaving program units →  Calling Processing Blocks →  Calling Procedures →  Method Calls →  Static Method Calls 

... meth1( ... )->meth2( ... )->... - method chaining

Other versions: 7.31 | 7.40 | 7.54

Syntax


... { meth( ... )->
meth1( ... )->meth2( ... )->...->methn( ... ) } 
  | { meth( ... )->meth1( ... )->meth2( ... )->...->attr        } ...

Alternatives

1. meth( ... )->meth1( ... )->meth2( ... )->...->methn( ... )

2. meth( ... )->meth1( ... )->meth2( ... )->...->attr

Effect

Chains static method calls to a chained method call or a chained attribute access. meth, meth1, meth2, ... , expect functional methods whose return values are reference variables pointing to objects with the next method in question. All methods that follow meth must be called using the object component selector.

The parameters are passed to the functional methods meth, meth1, meth2, ... using the syntax valid for functional method calls.

Alternative 1

meth( ... )->meth1( ... )->meth2( ... )->...->methn( ... )

Effect

Chained method call. Calls the instance method methn in an object. The reference variable for the object is the return value of the preceding method chaining.

A chained method call can be specified as a standalone statement or as a functional method call in a suitable operand position. The relevant rules apply in parameter passing to meth.

If the return value of the last method has a structured data type, the chained method call can, like a structure, be specified in front of the structure component selector - and use this to access a component of the structure.


Note

In methn, a constructor expression can be specified with a constructor operator NEW or CAST for oref not only in a functional call, but also in standalone statements.


Example

Calls the method m3 in an object of the class c3, addressed using method chaining.

CLASS c3 DEFINITION. 
  PUBLIC SECTION. 
    METHODS m3 CHANGING c3 TYPE string. 
ENDCLASS. 

CLASS c2 DEFINITION. 
  PUBLIC SECTION. 
    METHODS m2  IMPORTING i2 TYPE string 
                RETURNING value(r2) TYPE REF TO c3. 
ENDCLASS. 

CLASS c1 DEFINITION. 
  PUBLIC SECTION. 
    CLASS-METHODS m1 IMPORTING i1 TYPE string 
                     RETURNING value(r1) TYPE REF TO c2. 
ENDCLASS. 

CLASS c1 IMPLEMENTATION. 
  METHOD m1. 
    CREATE OBJECT r1. 
  ENDMETHOD. 
ENDCLASS. 

CLASS c2 IMPLEMENTATION. 
  METHOD m2. 
    CREATE OBJECT r2. 
  ENDMETHOD. 
ENDCLASS. 

CLASS c3 IMPLEMENTATION. 
  METHOD m3. 
    c3 = 'New text'. 
    MESSAGE c3 TYPE 'I'. 
  ENDMETHOD. 
ENDCLASS. 

START-OF-SELECTION. 
  DATA txt TYPE string VALUE `test`. 
  c1=>m1( 
    i1 = `p1` )->m2( 
    i2 = `p2` )->m3( 
    CHANGING c3 = txt ). 

Alternative 2

meth( ... )->meth1( ... )->meth2( ... )->...->attr

Effect

Chained attribute access. Accesses the instance attribute attr in an object. The reference variable for the object is the return value of the preceding method chaining.

If the attribute has a structured data type, the chained attribute access can, like a structure, be specified in front of the structure component selector - and use this to access a component of the structure.

Chained attribute access can currently only be specified in suitable reading positions. Writes to an attribute addressed using method chaining are not yet possible.


Example

Chained attribute access in executable example program DEMO_METHOD_CHAINING.

cl_demo_output=>display(
  oref->meth( `Hello ` )->meth( `world` )->meth( `!` )->text ).

Executable Example

Method Chaining

Continue

Method Chaining