Difference between static and instance variables

by Vikram Chellappa

Static attributes:

  1. The attributes are created only once.
  2. That means memory allocation for the attribute is only once.
  3. We can call next time the same attribute by using object then the same memory space is referred.
  4. We can change the value in the memory.

Instance attributes:

  1. These attributes are created for every time.
  2. That means for each and every call statement, the attribute creates a new memory location at every time.

Go to SE38 and create the following program.

*&---------------------------------------------------------------------*
*& Report  ZLOCALCLASS_VARIABLES                                       *
*&                                                                     *
*&---------------------------------------------------------------------*
*&      By                                                             *
*&         Vikram.C  for SAPTechnical.COM                              *
*&---------------------------------------------------------------------*
REPORT  ZLOCALCLASS_VARIABLES.
*General local class by using static and instance.
*Define the class
CLASS CL_LC DEFINITION.
PUBLIC SECTION.
DATA: A TYPE I VALUE 10.
CLASS-DATA: B TYPE I VALUE 20.
METHODS DISPLAY.
CLASS-METHODS DISP.
ENDCLASS.
*Implement the class
CLASS CL_LC IMPLEMENTATION.
METHOD DISPLAY.
WRITE:/ 'IT IS INSTANCE METHOD',
      / 'INSTANCE VARIABLE = ', A.
ENDMETHOD.
METHOD DISP.
WRITE:/ 'IT IS STATIC METHOD' COLOR 2,
      / 'STATIC VARIABLE = ', B COLOR 2.
ENDMETHOD.
ENDCLASS.
*Create the object
DATA OBJ TYPE REF TO CL_LC.
START-OF-SELECTION.
CREATE OBJECT OBJ.
*Call the instance method
CALL METHOD OBJ->DISPLAY.
*Call the static method.
CALL METHOD CL_LC=>DISP. 

Save it, check it, activate it after that execute it.

Then the output is like this. 



 

Please send us your feedback/suggestions at webmaster@SAPTechnical.COM 

HomeContribute About Us Privacy Terms Of Use • Disclaimer • SafeCompanies: Advertise on SAPTechnical.COM | Post JobContact Us  

Graphic Design by Round the Bend Wizards

footer image footer image