METHOD

Basic form

METHOD meth.

Effect

Implementation of a method in a class of ABAP objects.

This statement is only allowed in the implementation part of a class declaration (see CLASS class IMPLEMENTATION). A complete method implementation consists of a statement block.

METHOD meth.
...
ENDMETHOD.

Methods belong to the components of a class. All the instance and static methods of a class must be implemented with such a statement block. This is also true of the methods of the interfaces that are implemented by the class.

Methods work on the attributes of a class, but can also have local data. The local data consists partly of the interface parameters of the method. The interface of a method is defined in the declaration of the method with the METHODS or CLASS-METHODS statement and for event handling methods by declaring the corresponding event with the EVENTS or CLASS-EVENTS statement.

As in other procedures (subroutines and function modules), methods can also declare further local data types and data objects with the TYPES and DATA statement. Local data types and objects shadow attributes of the class with the same name. Whereas hidden global data cannot be addressed in subroutines and function modules, hidden attributes of the class can always be addressed in a method with the self-reference ME->.

Example

CLASS C1 DEFINITION.
  PUBLIC SECTION.
    DATA A1 TYPE C.
    METHODS M1.
  PRIVATE SECTION.
    METHODS: M2, M3 ...
ENDCLASS.

DATA O1 TYPE REF TO C1.
CREATE OBJECT O1.
O1->M1( ).

CLASS C1 IMPLEMENTATION.
  METHOD M1.
    M2( ).
    M3( ).
  ENDMETHOD.
  METHOD M2.
    DATA: M2_NUMBER TYPE I,
          A1 TYPE C.
    MOVE ME->A1 TO A1.
    ...
  ENDMETHOD.
  METHOD M3.
    ...
  ENDMETHOD.
ENDCLASS.

This example shows the implementation of three methods of class C1. In method M2, a local data object A1 hides the class attribute with the same name, but the class attribute can be addressed with the self-reference ME->A1.

Related

CLASS

PUBLIC SECTION

PROTECTED SECTION

PRIVATE SECTION

ENDCLASS

CLASS-DATA

CLASS-METHODS

CLASS-EVENTS

METHODS

EVENTS

INTERFACES

ALIASES

METHOD

ENDMETHOD

INTERFACE

ENDINTERFACE

CREATE OBJECT

CALL METHOD

RAISE EVENT

SET HANDLER

Special variants of other keywords

Additional help

Declaring and Calling Methods