Skip to content

ABAP Keyword Documentation →  ABAP − Reference →  Calling and leaving program units →  Calling Processing Blocks →  Calling Procedures →  Method Calls →  Dynamic Method Call →  CALL METHOD 

CALL METHOD - dynamic_meth

Quick Reference

Other versions: 7.31 | 7.40 | 7.54

Syntax


... (meth_name) 
  | oref->(meth_name)
  | class=>(meth_name)
  | (class_name)=>(meth_name)
  | (class_name)=>meth ...

Extras

1. ... (meth_name) ...

2. ... (class_name) ...

Alternatives

1. ... (meth_name) ...

2. ... oref->(meth_name) ...

3. ... class=>(meth_name) ...

4. ... (class_name)=>(meth_name) ...

5. ... (class_name)=>meth ...

Effect

These names are used to specify methods dynamically.

Addition 1

... (meth_name) ...

Effect

meth_name expects a character-like field that must contain the name of a method when the statement is executed.

Addition 2

... (class_name) ...

Effect

class_name expects a character-like field that must contain the name of a class in uppercase letters when the statement is executed. An absolute type name can also be specified. The following can be specified for class_name:

  • Literal or constant
If the data object class_name is specified as a character literal or as a constant, it can be evaluated statically and the specified class is identified as the used object.
  • Variable
If the data object class_name is specified as a variable, it is specified only dynamically and the content is not evaluated statically.

When the statement is executed, class_name is not evaluated until runtime (in both cases).

Alternative 1

... (meth_name) ...

Effect

This variant is only possible for methods of the same class. It has the same effect as me->(meth_name) (see alternative 2).

Alternative 2

... oref->(meth_name) ...

Effect

This form may be used for all visible methods of objects. oref can be any class reference variable or interface reference variable that points to an object containing the method or interface method specified in meth_name. This method is searched for first in the static type, then in the dynamic type of oref


Note

In the dynamic case too, only interface components can be accessed and it is not possible to use interface reference variable to access any type of component.

Alternative 3

... class=>(meth_name) ...

Alternative 4

... (class_name)=>(meth_name) ...

Alternative 5

... (class_name)=>meth ...

Effect

These forms are possible for all visible static methods. Both the class and method can be specified dynamically. The class class and the method meth can also be specified directly.

In the alternatives with a dynamic class name (class_name), first the class is searched for, then the method. If class is specified statically, the search for the method is carried out in the existing class.


Notes

  • If, in class_name, a class of another program is specified using an absolute type name, this program is loaded into a new additional program group or into the current program group, depending on the program type (if not already loaded). If required, the program constructor is also executed.
  • External calls of local class methods is critical, especially for executable programs, module pools, and subroutine pools, since it is not usually possible to define statically to which program group the framework group is assigned.
  • Methods of local classes can be called externally only by specifying the compilation unit. In the case of classes defined in an include program, the name of the include program cannot be used.

Example

This example demonstrates various types of dynamic method calls.

CLASS cls DEFINITION. 
  PUBLIC SECTION. 
    CLASS-METHODS meth1. 
  PRIVATE SECTION. 
    METHODS: meth2, 
      meth3. 
ENDCLASS. 

CLASS cls IMPLEMENTATION. 
  METHOD meth1. 
    DATA(oref) = NEW cls( ). 
    DATA(meth) = `METH2`. 
    CALL METHOD oref->(meth). 
  ENDMETHOD. 
  METHOD meth2. 
    DATA(meth) = `METH3`. 
    CALL METHOD (meth). 
  ENDMETHOD. 
  METHOD meth3. 
    cl_demo_output=>display( 'called' ). 
  ENDMETHOD. 
ENDCLASS. 

START-OF-SELECTION. 
  DATA(class) = `CLS`. 
  DATA(meth) = `METH1`. 
  CALL METHOD (class)=>(meth).