Field symbol can be used in object oriented programming. The field symbol contains the value of any variable. The variable can be normal instance or any static variable in a class. In the following example we have declared an instance variable v_ins & a static variable v_cls in class cl_fs_test. Now we declare the field symbol at the global declaration part. After that we assign the variables to the field symbol one by one.
REPORT zsr_test NO STANDARD PAGE HEADING.
FIELD-SYMBOLS <fs> TYPE ANY.
*----------------------------------------------------------------------*
* CLASS cl_fs_test DEFINITION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS cl_fs_test DEFINITION.
PUBLIC SECTION.
DATA v_ins TYPE char50 VALUE 'Assign Instance variable to FS'.
CLASS-DATA v_cls TYPE char50 VALUE 'Assign static variable to FS'.
ENDCLASS. "cl_fs_test DEFINITION
*----------------------------------------------------------------------*
* CLASS cl_fs_test IMPLEMENTATION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS cl_fs_test IMPLEMENTATION.
ENDCLASS. "cl_fs_test IMPLEMENTATION
START-OF-SELECTION. DATA obj TYPE REF TO cl_fs_test.
CREATE OBJECT obj.
ASSIGN obj->v_ins TO <fs>.
WRITE / <fs>.
ASSIGN obj->v_cls TO <fs>.
WRITE / <fs>.
cl_fs_test=>v_cls = 'Changing static variable to FS'.
ASSIGN cl_fs_test=>v_cls TO <fs>.
WRITE: / <fs>.
FIELD-SYMBOLS <fs> TYPE ANY.
*----------------------------------------------------------------------*
* CLASS cl_fs_test DEFINITION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS cl_fs_test DEFINITION.
PUBLIC SECTION.
DATA v_ins TYPE char50 VALUE 'Assign Instance variable to FS'.
CLASS-DATA v_cls TYPE char50 VALUE 'Assign static variable to FS'.
ENDCLASS. "cl_fs_test DEFINITION
*----------------------------------------------------------------------*
* CLASS cl_fs_test IMPLEMENTATION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS cl_fs_test IMPLEMENTATION.
ENDCLASS. "cl_fs_test IMPLEMENTATION
START-OF-SELECTION. DATA obj TYPE REF TO cl_fs_test.
CREATE OBJECT obj.
ASSIGN obj->v_ins TO <fs>.
WRITE / <fs>.
ASSIGN obj->v_cls TO <fs>.
WRITE / <fs>.
cl_fs_test=>v_cls = 'Changing static variable to FS'.
ASSIGN cl_fs_test=>v_cls TO <fs>.
WRITE: / <fs>.
The output is as follows:
Another version of this requirement is as follows. Here we have declared a method where we are assigning the instance variable and static component to the field symbol <fs>. Now at user level after calling this method we modify the static component and write this.
REPORT zsr_test NO STANDARD PAGE HEADING.
FIELD-SYMBOLS <fs> TYPE ANY.
*----------------------------------------------------------------------*
* CLASS cl_fs_test DEFINITION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS cl_fs_test DEFINITION.
PUBLIC SECTION.
DATA v_txt TYPE char40 VALUE 'Instance Variable'.
CLASS-DATA cl_v_txt TYPE char40 VALUE 'Static Variable'.
METHODS m_fs_test.
ENDCLASS. "cl_fs_test DEFINITION
*----------------------------------------------------------------------*
* CLASS cl_fs_test IMPLEMENTATION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS cl_fs_test IMPLEMENTATION.
METHOD m_fs_test.
ASSIGN v_txt TO <fs>.
WRITE: / <fs>.
ASSIGN cl_v_txt TO <fs>.
WRITE: / <fs>.
ENDMETHOD. "m_fs_test
ENDCLASS. "cl_fs_test IMPLEMENTATION
START-OF-SELECTION. DATA obj TYPE REF TO cl_fs_test.
CREATE OBJECT obj.
CALL METHOD obj->m_fs_test.
cl_fs_test=>cl_v_txt = 'Static Variable from User'.
WRITE: / <fs>.
FIELD-SYMBOLS <fs> TYPE ANY.
*----------------------------------------------------------------------*
* CLASS cl_fs_test DEFINITION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS cl_fs_test DEFINITION.
PUBLIC SECTION.
DATA v_txt TYPE char40 VALUE 'Instance Variable'.
CLASS-DATA cl_v_txt TYPE char40 VALUE 'Static Variable'.
METHODS m_fs_test.
ENDCLASS. "cl_fs_test DEFINITION
*----------------------------------------------------------------------*
* CLASS cl_fs_test IMPLEMENTATION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS cl_fs_test IMPLEMENTATION.
METHOD m_fs_test.
ASSIGN v_txt TO <fs>.
WRITE: / <fs>.
ASSIGN cl_v_txt TO <fs>.
WRITE: / <fs>.
ENDMETHOD. "m_fs_test
ENDCLASS. "cl_fs_test IMPLEMENTATION
START-OF-SELECTION. DATA obj TYPE REF TO cl_fs_test.
CREATE OBJECT obj.
CALL METHOD obj->m_fs_test.
cl_fs_test=>cl_v_txt = 'Static Variable from User'.
WRITE: / <fs>.