ABAP Keyword Documentation → ABAP - Reference → Program structure → Modularization Statements → Procedures → Methods
METHOD
Other versions: 7.31 | 7.40 | 7.54
Syntax
METHOD meth.
...
ENDMETHOD.
Effect
Between the statements METHOD
and ENDMETHOD
the function of a
method declared with
[CLASS-]
METHODS
meth
is implemented in a class. The implementation of a method is only possible
in an implementation part of a class that begins with CLASS class IMPLEMENTATION
Local data types and data objects can be declared within the method. It is also possible to access the
formal parameters of the method and all the components of all instances of its own class. The method
and its interface are defined either using the statement [CLASS-]METHODS
for a local class, or in the Class Builder tool for a global class.
In instance methods, all components of the method's class and the methods instance's can also be addressed explicitly using the self reference me->, as well as using their names. In addition, all components of other instances from the method's class can be addressed using reference variables.
A method can be called statically or dynamically. For static calls, both standalone and functional call forms are available. Dynamic calls are always standalone calls.
Note
-
When a method of an interface
intf
is implemented,meth
can be specified either as the name declared in the interface (prefixed withintf~
) or as an alias name of the class defined usingALIASES
. The method must exist in the interface; otherwise a syntax error is produced. Ifintf~
is used, only a syntax warning will appear for global interfaces, so that classes are not immediately rendered invalid if an unused method is deleted from a global interface. -
The addition BY
DATABASE PROCEDURE transforms a method implemented in a database-specific language (and not in ABAP) and executed in the database system to an
AMDP method.
Example
In this example, the two methods m1
and m2
of
the class c1
between METHOD
and ENDMETHOD
are implemented. Although the local data object a1
hides the attribute of the same name, the attribute a1
can be addressed using me->a1
.
CLASS c1 DEFINITION.
PUBLIC SECTION.
METHODS m1 IMPORTING p1 TYPE string.
PRIVATE SECTION.
DATA a1 TYPE string.
METHODS m2.
ENDCLASS.
CLASS c1 IMPLEMENTATION.
METHOD m1.
a1 = p1.
m2( ).
ENDMETHOD.
METHOD m2.
DATA a1 TYPE string.
a1 = me->a1.
ENDMETHOD.
ENDCLASS.