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
DATA: gv_instance_value TYPE i.
METHODS: increase_instance_value.
" Static attribute and method
CLASS-DATA: gv_static_value TYPE i.
CLASS-METHODS: increase_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_obj1) = NEW 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_obj2) = NEW 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 :