Preface – This post is part of the Object Oriented ABAP series.
Table of Contents
A method defines the behaviour of an object. It can access all the attributes and hence can change the status of the object. In this article we will discuss Class Method in SAP OOABAP.
There are a few points to consider while declaring the method:
- The method definition is defined in the class definition and implemented in class implementation.
- It can be declared using Method and End method statement.
- Methods have parameter interface to pass the values when they are called and return the value to the caller.
The method must be declared between the statements METHOD and END METHOD.
There are three types of methods:
These methods are declared using METHOD statements. They can access all the attributes of the class and can trigger all the events.
These types of method are declared using CLASS-METHOD statements. They can access the static attributes and can trigger static events only.
There are two special methods : constructor and class_constructor, which are called automatically whenever the class is accessed for the first time.
ME Operator in Class Method
In the implementation part of the instance method in a class, an implicitly created local reference variable ME is available. It points to the currently executed instance. The me variable is reserved and cannot be used for naming attributes and parameter.
****class definition**** CLASS zdemo DEFINITION. PUBLIC SECTION. DATA var(50) TYPE C VALUE “Displaying me variable”. METHOD instance_method. “Instance Method definition CLASS-METHODS static_method. “Static Method definition END CLASS. ****class implementation**** CLASS zdemo IMPLEMENTATION. METHOD instance_method. “Instance Method implementation WRITE : ME->var. “ Displaying ME Variable WRITE : “This is instance method” END METHOD. METHOD static_method. “Static Method implementation WRITE : “This is static method” END METHOD. END CLASS. ****creating class object**** START-OF-SELECTION DATA : object_demo TYPE REF TO zdemo. CREATE OBJECT object_demo. CALL METHOD : object_demo-> instance_method , object_demo->static_method.