Skip to content

ABAP Keyword Documentation →  ABAP − Reference →  Declarations →  Declaration Statements →  Classes and Interfaces →  Components in Classes and Interfaces →  Methods 

METHODS

Quick Reference

Other versions: 7.31 | 7.40 | 7.54

Syntax Forms


General Instance Methods

1. METHODS meth [ABSTRACT|FINAL]
              |[DEFAULT IGNORE|FAIL]

    [IMPORTING parameters [PREFERRED PARAMETER p]]
    [EXPORTING parameters]
    [CHANGING  parameters]
    [{RAISING exc1|RESUMABLE(exc1) exc2|RESUMABLE(exc2) ...}
    |{EXCEPTIONS exc1 exc2 ...}].

Functional Instance Methods

2. METHODS meth [ABSTRACT|FINAL]
              |[DEFAULT IGNORE|FAIL]

    [IMPORTING parameters [PREFERRED PARAMETER p]]
    [EXPORTING parameters]
    [CHANGING  parameters]
    RETURNING VALUE(r) typing
    [{RAISING exc1|RESUMABLE(exc1) exc2|RESUMABLE(exc2) ...}
    |{EXCEPTIONS exc1 exc2 ...}].

Instance Constructors (Only in Classes)

3. METHODS constructor [FINAL]
    [IMPORTING parameters [PREFERRED PARAMETER p]]
    [{RAISING exc1|RESUMABLE(exc1) exc2|RESUMABLE(exc2) ...}
    |{EXCEPTIONS exc1 exc2 ...}].

Event Handlers

4. METHODS meth [ABSTRACT|FINAL]
              |[DEFAULT IGNORE|FAIL]

     FOR EVENT evt OF {class|intf}
     [IMPORTING p1 p2 ... [sender]].

Redefinition of Instance Methods (Only in Subclasses)

5. METHODS meth [FINAL] REDEFINITION.

AMDP Methods

6. METHODS meth [ABSTRACT|FINAL]
              |[DEFAULT IGNORE|FAIL]

               [AMDP OPTIONS [READ-ONLY] [CDS SESSION CLIENT clnt|CURRENT]]
    [IMPORTING parameters [PREFERRED PARAMETER p]]
    [EXPORTING parameters]
    [CHANGING  parameters]
    [RETURNING VALUE(r) typing]
    [{RAISING exc1|RESUMABLE(exc1) exc2|RESUMABLE(exc2) ...}]

Test Methods (Only in Test Classes)

7. METHODS meth [ABSTRACT|FINAL]
               [AMDP OPTIONS [READ-ONLY] [CDS SESSION CLIENT clnt|CURRENT]]
    FOR TESTING
    [{RAISING exc1|RESUMABLE(exc1) exc2|RESUMABLE(exc2) ...}].

Effect

The statement METHODS declares or redefines an instance method meth. The naming conventions apply to the name meth.

Instance methods are bound to objects. To use instance methods, an object of the class must first be created. In instance methods, all components of the same class can be accessed without a component selector.

The variants of the statement METHODS are used to distinguish between the following kinds of method declarations:

  • General instance methods
    The general form of the METHODS statement allows instance methods to be defined with any input and output parameters.
  • Functional instance methods
    Functional methods have exactly one return value and any number of formal parameters.
  • Instance constructors
    Instance constructors are methods with the given name constructor, which are called automatically when their class is instantiated. Constructors have any number of input parameters and no output parameters. They cannot be declared in interfaces.
  • Event handlers
    Event handlers are methods that can be called directly using statements (or in statements), but are mainly called when an event of a class or an interface is raised. The only possible formal parameters of an event handler are input parameters, which have been defined as the output parameters of the event.
  • Redefinition of Instance Methods
    A method declared in a superclass can be redefined in a subclass as long as it is not flagged as final in the superclass. In a redefinition, the interface of the method is not changed.
  • AMDP Methods
    In general, the same rules apply when declaring AMDP methods as in all instance methods. There is, however, one special optional addition, AMDP OPTIONS, and no non-class-based exceptions can be declared using EXCEPTIONS.
  • Definition of a test method
    Test methods can be declared in test classes. They have no interface parameters and are called during ABAP Unit tests by the ABAP runtime environment. They cannot be declared in interfaces.


Note

In the methods of a class, one method of the class obscures a built-in function with the same name. This is done regardless of the type and number of method parameters and the type and number of arguments on the function. This means that a method should not have the same name as a built-in function.

Continue

METHODS - ABSTRACT, FINAL

METHODS - DEFAULT

METHODS - IMPORTING, EXPORTING, CHANGING, RAISING

METHODS - RETURNING

METHODS - constructor

METHODS - FOR EVENT

METHODS - REDEFINITION

METHODS - parameters