Hello,

Sign up to join our community!

Welcome Back,

Please sign in to your account!

Forgot Password,

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

You must login to ask a question.

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

SAP EWM Help Latest Questions

  • 0
  • 0
DPM125

What is the difference between static and instance Members in SAP OOABAP ?

Instance Members (Methods & Data)

  • Belong to individual objects (instances).
  • Created using DATA and METHODS.
  • Each object has its own copy of the data.
  • Accessed using the object reference: lo_obj->method_name.
  • Useful when each object should maintain its own state.
  • Destroyed when the object goes out of scope.

Static Members (Methods & Data)

  • Belong to the class itself, not any particular object.
  • Created using CLASS-DATA and CLASS-METHODS.
  • Only one copy exists for the entire class.
  • Accessed using the class name: zcl_class_name=>method_name.
  • Useful for shared data, like counters, configuration, or logging.
  • Exist as long as the class is loaded in memory.
*&---------------------------------------------------------------------*
*& Report  ZREP_LCLCLASS
*&
*&---------------------------------------------------------------------*
*&
*&
*&---------------------------------------------------------------------*

REPORT zrep_lclclass.

CLASS zcl_static_instance_demo DEFINITION.
  PUBLIC SECTION.
    " Instance attribute and method
    DATAgv_instance_value TYPE i.
    METHODSincrease_instance_value.

    " Static attribute and method
    CLASS-DATAgv_static_value TYPE i.
    CLASS-METHODSincrease_static_value.
ENDCLASS.

CLASS zcl_static_instance_demo IMPLEMENTATION.

  METHOD increase_instance_value.
    gv_instance_value gv_instance_value + 1.
    WRITE/ 'Instance Value:'gv_instance_value.
  ENDMETHOD.

  METHOD increase_static_value.
    gv_static_value gv_static_value + 1.
    WRITE/ 'Static Value:'gv_static_value.
  ENDMETHOD.

ENDCLASS.

START-OF-SELECTION.

  WRITE/ '--Static Method Calls (No object needed---'.
  zcl_static_instance_demo=>increase_static_value)" 1
  zcl_static_instance_demo=>increase_static_value)" 2

  SKIP.

  WRITE/ '--Instance Method Calls (Using object---'.
  DATA(lo_obj1NEW zcl_static_instance_demo).
  lo_obj1->increase_instance_value)" 1
  lo_obj1->increase_instance_value)" 2

  SKIP.

  WRITE/ '--Another Object with Separate Instance ---'.
  DATA(lo_obj2NEW zcl_static_instance_demo).
  lo_obj2->increase_instance_value)" 1 (separate from lo_obj1)

  SKIP.

  WRITE/ '--Static Again (Still shared---'.
  zcl_static_instance_demo=>increase_static_value)" 3 

Output :

Related Questions

Leave an answer

Leave an answer