Write First SAP class

Writing first SAP Class using transaction code SE24 with SAP ABAP Programming

+ -
Create a simple class for using attributes

Go to SE24( Class Builder).

Give the class name ZCL_SAPN1 and create.

creating a class in SAP

Provide short description, save.

Save it in a Local Object or in your test package.

Go to attributes tab and define an attribute as below.

CReate a class in SE24

AV_NAME-INSTANCE-PUBLIC-TYPE-CHAR25.

Save and Activate.

Now class is created, we have to use this in our program.

Create a ABAP program in SE38 and add below code

DATA : LR_CLASS TYPE REF TO ZCL_SAPN1 . "STEP1--WE DECLARE CLASSES USING REF TO BECAUSE THEY ARE OBJECTS
CREATE OBJECT LR_CLASS. "STEP2--CREATE OBJECT FOR THE CLASS
*CALL CLASS COMPONENT WITH THE INSTANCE 
LR_CLASS->AV_NAME = 'ATTRIBUTE NAME'. "USE CLASS COMPONENTS
WRITE:/ LR_CLASS->AV_NAME.
*OUT PUT WILL BE 'ATTRIBUTE NAME'

Using multiple objects of class

DATA : LR_CLASS1 TYPE REF TO ZCL_SAPN1 . "Declare first class object
DATA : LR_CLASS2 TYPE REF TO ZCL_SAPN1 . "Declare second class object

CREATE OBJECT LR_CLASS1. "Create a first object
CREATE OBJECT LR_CLASS2. "Create a second object
*CALL CLASS COMPONENT WITH THE INSTANCE 
LR_CLASS1->AV_NAME = 'FIRST ATTRIBUTE NAME'. "Assign value to first object

LR_CLASS2->AV_NAME = 'SECOND ATTRIBUTE NAME'. "Assign value to second object
WRITE:/ LR_CLASS1->AV_NAME. "OUT PUT WILL BE 'FIRST ATTRIBUTE NAME'

WRITE:/ LR_CLASS2->AV_NAME. "OUT PUT WILL BE 'SECOND ATTRIBUTE NAME'

Learner Questions


No Questions by learners, be first one to ask ..!!

    Please Sign in to ask a question

    Was this lesson helpful to you? Yes No 100 People out of 102 think this lesson helpful


    Lesson Navigation

    ←Previous Chapter
    Next Lesson → Working with class methods in OOABAP