Friday, March 15, 2013

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.

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.

6 comments:

yektek training said...

nice post thank you

Bhavya Kumar said...

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

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

gilledashivani said...

Informative article. Thank you for sharing the great blog. You have done great explanation and visualization. Keep up the good work.
BEST SAP ABAP TRAINING IN HYDERABAD

lakshmi said...

Thanks for sharing the valuable information here. sap training institutes in hyderabad in information with this content. Thank you and please keep update like this informative details.
http://www.sapschool.in/