Local class with attributes in OOABAP

Creating local class with attributes in SAP ABAP programming, defining attributes in SAP local classes

+ -

Local class is a class definition and implementation is available in a program.

Go to SE38, create a program ZSAPN_CLASS_ATTRIBUTES and follow steps to add code

Define a class

CLASS CL_ATTRIBUTES DEFINITION. "DOUBLE CLICK ON CLASS NAME TO CREATE IMPLEMENTATION
PUBLIC SECTION.
DATA : PA_NAME TYPE CHAR25. "PUBLIC INSTANCE ATTRIBUTE
CLASS-DATA : PA_NAME_ST TYPE CHA25. "PUBLIC STATIC ATTRIBUTE
ENDCLASS.

Implement class

CLASS CL_ATTRIBUTES IMPLEMENTATION.
*LEAVE BLANK FOR THIS EXAMPLE
ENDCLASS.

Using class

DATA : LO_ATTRIBUTES TYPE REF TO CL_ATTRIBUTES. "DECLARE CLASS
CREATE OBJECT LO_ATTRIBUTES. "CREATE OBJECT


LO_ATTRIBUTES->PA_NAME = 'SACHNTHENDULAKR'. "ASSIGN A VALUE TO INSTANCE ATTRIBUTE
CL_ATTRIBUTES=>PA_NANE_ST = 'PONTING'. "ASSIGN VALUE TO STATIC ATTRIBUTES

*PRINT ATTRIBUTES VIA CLASS
WRITE:/ 'INSTANCE ATTRIBUTE:', LO_ATTRIBUTES->PA_NAME.
WRITE:/ 'STATIC ATTRIBUTE :', CL_ATTRIBUTE=>PA_NAME_ST.

DEFINITION DEFERED is a keyword which indicates the class definition is delayed or postponed or definition at some place in program.

Final code will be

CLASS CL_ATTRIBUTES DEFINITION DEFERED.
DATA : LO_ATTRIBUTES TYPE REF TO CL_ATTRIBUTES. "DECLARE CLASS
CREATE OBJECT LO_ATTRIBUTES. "CREATE OBJECT

CLASS CL_ATTRIBUTES DEFINITION. "DOUBLE CLICK ON CLASS NAME TO CREATE IMPLEMENTATION
PUBLIC SECTION.
DATA : PA_NAME TYPE CHAR25. "PUBLIC INSTANCE ATTRIBUTE
CLASS-DATA : PA_NAME_ST TYPE CHA25. "PUBLIC STATIC ATTRIBUTE
ENDCLASS.

LO_ATTRIBUTES->PA_NAME = 'SACHNTHENDULAKR'. "ASSIGN A VALUE TO INSTANCE ATTRIBUTE
CL_ATTRIBUTES=>PA_NANE_ST = 'PONTING'. "ASSIGN VALUE TO STATIC ATTRIBUTES

*PRINT ATTRIBUTES VIA CLASS
WRITE:/ 'INSTANCE ATTRIBUTE:', LO_ATTRIBUTES->PA_NAME.
WRITE:/ 'STATIC ATTRIBUTE :', CL_ATTRIBUTE=>PA_NAME_ST.
CLASS CL_ATTRIBUTES IMPLEMENTATION.
*LEAVE BLANK FOR THIS EXAMPLE
ENDCLASS.

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 21 People out of 22 think this lesson helpful


    Lesson Navigation

    ←Previous Chapter
    Next Lesson → Local Classes with methods in OOABAP