Tuesday, December 8, 2015

Field Symbols in Class



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>.

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>.


3 comments:

Unknown said...

This information which you provided is very much useful for us.It was very interesting and useful for SAP online training.We also providing
SAP Help Portal | SAP Online Training Tutorials

Unknown said...

Thanks for sharing great information in your blog. Got to learn new things from your Blog. It was very nice blog to learn about sap abap training in chennai

Anonymous said...

SAP Success Factors Real Time Hands on Training in Chennai...

Don't always Depend on Training Institute Alone and so please aware of Best Trainers too..

http://thecreatingexperts.com/sap-successfactors-training-in-chennai/

If You need a Best Trainer over SAP Success Factors Means??? Please ready for an DEMO From the Trainer MR.Karthick
CONTACT:8122241286

Both Classroom/Online Training is Available!!!!!!