0% found this document useful (0 votes)
28 views2 pages

Method Example

The document defines a report in ABAP that includes a class 'lcl_abc' with an instance method 'm1' and a static method 'm2'. It demonstrates how to call these methods, highlighting that instance methods require an object reference while static methods can be called directly from the class. The implementation includes sample method calls and comments on syntax errors related to method access.

Uploaded by

kishancp
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
28 views2 pages

Method Example

The document defines a report in ABAP that includes a class 'lcl_abc' with an instance method 'm1' and a static method 'm2'. It demonstrates how to call these methods, highlighting that instance methods require an object reference while static methods can be called directly from the class. The implementation includes sample method calls and comments on syntax errors related to method access.

Uploaded by

kishancp
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

*&---------------------------------------------------------------------*

*& Report ZSAP_TRAINING_CLASS_METHODS


*&---------------------------------------------------------------------*
*&
*&---------------------------------------------------------------------*
REPORT zsap_training_class_methods.

CLASS lcl_abc DEFINITION.

PUBLIC SECTION.

METHODS m1. "instance method


CLASS-METHODS m2. "static method

PROTECTED SECTION.
DATA gv1 TYPE i. " instance
CLASS-DATA gv2 TYPE i . "static
CONSTANTS gc TYPE i VALUE 500.

PRIVATE SECTION.

ENDCLASS.

CLASS lcl_abc IMPLEMENTATION.

METHOD m2. "static method


ULINE.
FORMAT COLOR 1.
* gv1 = 100.
gv2 = 200.

WRITE:/ 'Inside static method'.


* WRITE:/ gv1.
WRITE:/ gv2.
WRITE:/ gc.

ENDMETHOD.

METHOD m1." instance method


ULINE.
FORMAT COLOR 3.
gv1 = 100.
gv2 = 200.

WRITE:/ 'Inside instance method'.


WRITE:/ gv1.
WRITE:/ gv2.
WRITE:/ gc.
ENDMETHOD.

ENDCLASS.

START-OF-SELECTION.
*call method m1. "syntax error you can acess instance method only through object
reference.
*call method lcl_abc=>m1. " syntax error => can be used only with static methods
*
* DATA(obj_1) = NEW lcl_abc( ).
*
* CALL METHOD obj_1->m1." instance method call
* obj_1->m1( ).
*
*
* obj_1->m2( ).
* CALL METHOD obj_1->m2." static method call is possible using object reference
* call method lcl_abc=>m2. " also possible to access the static method using class
name.
*

data lr_obj type REF TO zcl_attribute.


CREATE OBJECT lr_obj.

* call method lr_obj->m1.

lr_obj->m1( ).

* lr_obj->m2( ).

zcl_attribute=>m2( ).

You might also like