Static Attribute
Static Attribute
Static attribute is declared with the statement CLASS-DATA. All the objects or instances can use the static attribute of the class. Validity of static attribute is not associated with the class itself not with the instances of the class.
Static attributes are accessed directly with the help of class name not interface name like cl_one=>cl_v_txt = 'SAP ABAP Dynpro'.
In the following example we have declared two static attributes. We want to display the text from the method m_one. Inside the method the text is 'SAP ABAP Object Oriented'. After that we are calling this static attribute directly with the class and edit it again to 'SAP ABAP Dynpro'. After that we shall print this directly.
REPORT zsr_test NO STANDARD PAGE HEADING.
*----------------------------------------------------------------------*
* CLASS cl_one DEFINITION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS cl_one DEFINITION.
PUBLIC SECTION.
CLASS-DATA: cl_v_txt TYPE char40,
cl_v_cnt TYPE i.
METHODS m_one.
ENDCLASS. "cl_one DEFINITION
*----------------------------------------------------------------------*
* CLASS cl_one IMPLEMENTATION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS cl_one IMPLEMENTATION.
METHOD m_one.
cl_v_txt = 'SAP ABAP Object Oriented'.
cl_v_cnt = 10.
DO cl_v_cnt TIMES.
WRITE: / sy-index, '>', cl_v_txt.
ENDDO.
SKIP.
ENDMETHOD. "m_one
ENDCLASS. "cl_one IMPLEMENTATION
START-OF-SELECTION.
DATA obj TYPE REF TO cl_one.
CREATE OBJECT obj.
CALL METHOD obj->m_one.
cl_one=>cl_v_txt = 'SAP ABAP Dynpro'.
cl_one=>cl_v_cnt = 5.
DO cl_one=>cl_v_cnt TIMES.
WRITE: / sy-index, '>', cl_one=>cl_v_txt.
ENDDO.
*----------------------------------------------------------------------*
* CLASS cl_one DEFINITION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS cl_one DEFINITION.
PUBLIC SECTION.
CLASS-DATA: cl_v_txt TYPE char40,
cl_v_cnt TYPE i.
METHODS m_one.
ENDCLASS. "cl_one DEFINITION
*----------------------------------------------------------------------*
* CLASS cl_one IMPLEMENTATION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS cl_one IMPLEMENTATION.
METHOD m_one.
cl_v_txt = 'SAP ABAP Object Oriented'.
cl_v_cnt = 10.
DO cl_v_cnt TIMES.
WRITE: / sy-index, '>', cl_v_txt.
ENDDO.
SKIP.
ENDMETHOD. "m_one
ENDCLASS. "cl_one IMPLEMENTATION
START-OF-SELECTION.
DATA obj TYPE REF TO cl_one.
CREATE OBJECT obj.
CALL METHOD obj->m_one.
cl_one=>cl_v_txt = 'SAP ABAP Dynpro'.
cl_one=>cl_v_cnt = 5.
DO cl_one=>cl_v_cnt TIMES.
WRITE: / sy-index, '>', cl_one=>cl_v_txt.
ENDDO.
Now we shall inspect at debugging level. We set breakpoint at CALL METHOD. There we can see cl_one=>cl_v_txt & cl_one=>cl_v_cnt hold nothing.
We enter into the method now and see the class data with respective values. We also can see the cl_one=>cl_v_txt holds the same value as inside the method.
Now after finishing the operation inside the method the program comes out. At this time the system doesn’t have access to the cl_v_txt but it holds value for cl_one=>cl_v_txt as per assignment.
Finally after finishing the operation here we get the output as follows.
Post a Comment