This section contains explains how to work with methods in ABAP Objects. For precise details of the relevant ABAP statements, refer to the corresponding keyword documentation in the ABAP Editor. The example shows how to declare, implement, and call methods.
You can declare methods in the declaration part of a class or in an interface. To declare instance methods, use the following statement:
METHODS meth IMPORTING
i2 ... [)] TYPE type
EXPORTING [VALUE(]e1 e2 ... [)] TYPE type ...
CHANGING [VALUE(]c1 c2 ... [)] TYPE type [OPTIONAL]...
EXCEPTIONS exc1 exc2 ... .
and the appropriate additions.
To declare static methods, use the following statement:
Both statements have the same syntax.
When you declare a method, you also define its parameter interface using the additions IMPORTING, EXPORTING, CHANGING, and RETURNING. The additions define the input, output, and input/output parameters, and the return code. They also define the attributes of the interface parameters, namely whether a parameter is to be passed by reference or value (VALUE), its type (TYPE), and whether it is optional (OPTIONAL, DEFAULT). Unlike in function modules, the default way of passing a parameter in a method is by reference. To pass a parameter by value, you must do so explicitly using the VALUE addition. The return value (RETURNING parameter) must always be passed explicitly as a value. This is suitable for methods that return a single output value. If you use it, you cannot use EXPORTING or CHANGING parameters.
As in function modules, you can use exception parameters (EXCEPTIONS) to allow the user to react to error situations when the method is executed.
You must implement all of the methods in a class in the implementation part of the class in a
block. When you implement the method, you do not have to specify any interface parameters, since these are defined in the method declaration. The interface parameters of a method behave like local variables within the method implementation. You can define additional local variables within a method using the DATA statement.
As in function modules, you can use the RAISE exception and MESSAGE RAISING statements to handle error situations.
When you implement a static method, remember that it can only work with the static attributes of your class. Instance methods can work with both static and instance attributes.
To call a method, use the following statement:
CALL METHOD meth EXPORTING i1 = f1 i2 =f2 ...
IMPORTING e1 =
CHANGING c1 = f1 c2 =f2 ...
RECEIVING r = h
EXCEPTIONS e1 = rc1 e2 =rc2 ...
The way in which you address the method method depends on the method itself and from where you are calling it. Within the implementation part of a class, you can call the methods of the same class directly using their name meth.
CALL METHOD meth...
Outside the class, the visibility of the method depends on whether you can call it at all. Visible instance methods can be called from outside the class using
CALL METHOD ref->meth...
where ref is a reference variable whose value points to an instance of the class. Visible instance methods can be called from outside the class using
CALL METHOD =>meth...
where class is the name of the relevant class.
When you call a method, you must pass all non-optional input parameters using the EXPORTING or CHANGING addition in the CALL METHOD statement. You can (but do not have to) import the output parameters into your program using the IMPORTING or RECEIVING addition. Equally, you can (but do not have to) handle any exceptions triggered by the exceptions using the EXCEPTIONS addition. However, this is recommended.
You pass and receive values to and from methods in the same way as with function modules, that is, with the syntax:
... Formal parameter = Actual parameter
after the corresponding addition. The interface parameters (formal parameters) are always on the left-hand side of the equals sign. The actual parameters are always on the right. The equals sign is not an assignment operator in this context; it merely serves to assign program variables to the interface parameters of the method.
If the interface of a method consists only of a single IMPORTING parameter, you can use the following shortened form of the method call:
CALL METHOD method( f ).
The actual parameter f is passed to the input parameters of the method.
If the interface of a method consists only of IMPORTING parameters, you can use the following shortened form of the method call:
CALL METHOD method( i1 = f1 i2 = f2 ...).
Each actual parameter f1 is passed to the corresponding formal parameter i1.
Using the standard ABAP parenthesis semantics you can call methods dynamically.
· Calling an instance method meth:
CALL METHOD ref->(f)
· Calling a static method meth:
CALL METHOD class=>(f)
CALL METHOD (c)=>meth
CALL METHOD (c)=>(f)
· Calling a user-defined method meth:
CALL METHOD (f)
CALL METHOD ME->(f)
Here, f and c are fields containing the name of the method meth or of the class class. Unlike subroutines and function modules, a dynamic method call allows you to pass the actual parameters dynamically as well. To do this, you use the additions PARAMETER-TABLE and EXCEPTION-TABLE of the CALL METHOD statement:
CALL METHOD ... PARAMETER-TABLE ptab
The parameter table ptab must be a hash table of the table type ABAP_PARMBIND_TAB or of the line type ABAP_PARMBIND. These types are defined in the ABAP Dictionary. The table has the following three columns:
· NAME for the name of the formal parameter
· KIND for the type of parameter passing
· VALUE of the type REF TO DATA for the value of the actual parameter
The NAME column is the unique table key. For each non-optional parameter you must fill exactly one line of the internal table. For each optional parameter you can do this, but are not forced to.
The type of parameter passing is determined in the declaration of the method called for each formal parameter. The contents of the KIND column may therefore be initial. If you want to check the type of parameter passing at runtime, you can assign one of the following constants from the global class CL_ABAP_OBJECTDESCR to the KIND column:
· CL_ABAP_OBJECTDESCR=>EXPORTING for EXPORTING parameters
· CL_ABAP_OBJECTDESCR=>IMPORTING for IMPORTING parameters
· CL_ABAP_OBJECTDESCR=>CHANGING for CHANGING parameters
· CL_ABAP_OBJECTDESCR=>RECEIVING for RECEIVING parameters
The descriptions depend on the view of the caller. If the specified and actual parameter types differ, the system generates the catchable runtime error DYN_CALL_METH_PARAM_KIND. You can also use the entries in the KIND column as an additional key, for example, to read all imported values after the method call. For the value of the actual parameter, the VALUE reference of the table line must point to a data object containing the requested value. You can use the command GET REFERENCE in this case.
The exception table etab must be a hash table of the table type ABAP_EXCPBIND_TAB or of the line type ABAP_EXCPBIND. These types are defined in the ABAP Dictionary. The table has the following two columns:
· NAME for the name of the exception
· VALUE of type i for the value to be assigned to sy-subrc
The NAME column is the unique table key. For each exception, you can fill exactly one line of the internal table. The VALUE component is assigned the numeric value to appear in sy-subrc after the exception is triggered.
Functional methods are methods with any number of IMPORTING parameters and one RETURNING parameter. In addition to CALL METHOD you can also use the following expressions at operand positions to call functional methods:
meth( f ) oder meth( p = f )
meth( p1 = f1 ... pn = fn )
This notation is currently supported:
· for the source field of the MOVE statement
· in arithmetic expressions of the COMPUTE statement
· in logical expressions
· in the CASE statement of the CASE control structure
· in the WHEN statement of the CASE control structure
· in the WHERE condition of the statements LOOP AT, DELETE and MODIFYfor internal tables
The functional method is entered instead of an operand. When the statement is executed, the method is called and the RETURNING parameter passed back is used as the operand.
Event handler methods are special methods that cannot all be called using the CALL METHOD statement. Instead, they are triggered using events. You define a method as an event handler method using the addition
... FOR EVENT evt OF cif ...
in the METHODS or CLASS-METHODS statement.
The following special rules apply to the interface of an event handler method:
· The interface may only consist of IMPORTING parameters.
· Each IMPORTING parameter must be an EXPORTING parameter of the event evt.
· The attributes of the parameters are defined in the declaration of the event evt (EVENTS statement) and are adopted by the event handler method.
See also Triggering and Handling Events
Constructors are special methods that cannot be called using CALL METHOD. Instead, they are called automatically by the system to set the initial state of a new object or class. There are two types of constructors - instance constructors and static constructors. Constructors are methods with a predefined name. To use them, you must declare them explicitly in the class.
The instance constructor of a class is the predefined instance method CONSTRUCTOR. You declare it in the public section as follows:
IMPORTING [VALUE(]i1 i2 ...[)] TYPE type [OPTIONAL]...
EXCEPTIONS exc1 exc2 ... .
and implement it in the implementation section like any other method. The system calls the instance constructor once for each instance of the class, directly after the object has been created in the CREATE OBJECT statement. You can pass the input parameters of the instance constructor and handle its exceptions using the EXPORTING and EXCEPTIONS additions in the CREATE OBJECT statement.
The static constructor of a class is the predefined static method CLASS_CONSTRUCTOR. You declare it in the public section as follows:
and implement it in the implementation section like any other method. The static constructor has no parameters. The system calls the static constructor once for each class, before the class is accessed for the first time. The static constructor cannot therefore access the components of its own class.
The methods example shows how to use instance and static constructors.