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 own class can be addressed not only using their name, but also explicitly using the self-reference me-> In addition, all the components of other instances of the same class can be addressed through reference variables.
A method can be called using the statement CALL METHOD
or using one of its abbreviated forms.
Note
Whenever a method of an interface
intf is implemented,
it is possible to specify for metheither the name declared in the interface
with a prefix intf or an alias name
of the class defined with ALIASES The method must exist in the interfface; otherwise, a syntax error will occur.
Example
If intf~is used, only a syntax warning will appear for global interfaces. The purpose
of this is so that classes are not immediately rendered invalid if an unused method from a global inteface
is deleted.In this example, the two methods m1
and m2
of the class METHOD
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.