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


8 comments:

Yaso Technosap 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

Ramya k said...

Thanks for the great information in your blog SAP HR Training in Chennai

susmi 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

Rahul said...

It was very nice blog to learn about SAP HR.Thanks for sharing new things.SAP HR

Bhavya Kumar said...

Best blog.Got to learn new things.Thanks for this Blog SAP Success Factors Training in Chennai

sanayamohit 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!!!!!!

Shruti Ramalingam said...

From My search…Creating Experts provides Best SAP Training with real time projects assistance.

Most of the modules are equipped with advance level topics which the student can learn from the basics to the advance level stage.

They also provide placement assistance in leading MNC companies across the globe according to the current requirements.

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

And these are the Best SAP training institute which provides Real Time Hands on Training…

codedion Technologies-9003085882

creating Experts-8122241286

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

Keerthi Suresh said...

SAP Success Factors Real Time Hands on Training in Chennai…

Don’t always Depend on Training Institute alone.Please aware of Best Trainers too..

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

If You need a Best Trainer in SAP Success Factors??? Then be ready for a DEMO From the Trainer MR.Karthick
CONTACT:8122241286
http://thecreatingexperts.com/sap-mm-training-in-chennai/

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