Dynamically call classes and methods
Now, learn how to dynamically invoke a method.
Let’s go for it.
How to invoke an instance method:
CALL METHOD ref->(f)
How to invoke a static method:
CALL METHOD class=>(f)
CALL METHOD (c)=>method
CALL METHOD (c)=>(f)
And how to invoke a method of the same class:
CALL METHOD (f)
CALL METHOD ME->(f)
Variable f is alphanumeric and holds the name of the method. Variable c is also alphanumeric and holds the name of the class.
The related instructions manual is here.
As if it weren’t enough, you can also pass through parameters dynamically, as follows:
CALL METHOD (class)=>(meth)
PARAMETER-TABLE
t_param.
where t_param is an internal table of type ABAP_PARMBIND_TAB.
The related instructions manual is here.
You’ll find an example of this here.
Very useful. Obviously, besides more confusing, since the code cannot be compiled, it will be forcibly slower than normal static calls. But it’s still good to know, because this can come in hand.
Update: Bruno Esperança points out, and I agree, that these dynamic calls should only be done strictly when necessary, since they will not be found through the “where-used” functionality.
Thanks, José Faria for showing me this.
Greetings from Abapinho.