SAP Tech Pro

SAP Technical Blog
Topprice Price Comparison

OO ABAP - Use of Static Attributes OO ABAP

OO ABAP - Use of Static Attributes OO ABAP

This program will demonstrate that Static attributes of a class are retained throughout the entire runtime. All the objects within a class can access its static attributes.

 

The program contains a class C1 with static attribute : NUM . The method : M1 increments the static attribute by 1 and displays the value each time it is called.

In the main START-OF-SELECTION portion, two objects : OBJ1 and OBJ2 are created from class C1.

First, static attribute : NUM is changed and accessed outside the class using the class component selector , ‘=>’.

Then, both objects OBJ1 and OBJ2 are used to call method : M1 which shows the new value of static attribute : NUM .

That the value of the static attribute gets incremented each time when the method M1 of different objects is called shows that this variable is able to retain its value through the entire runtime.


CLASS c1 DEFINITION .
  PUBLIC SECTION.
    CLASS-DATA : num TYPE i .
    METHODS : m1.
ENDCLASS.

CLASS c1 IMPLEMENTATION.
  METHOD m1 .
    num = num + 1.
    WRITE:/5 num .
  ENDMETHOD.
ENDCLASS.


START-OF-SELECTION.

  c1=>num = 3.
  WRITE:/5 c1=>num .

  DATA : oref1 TYPE REF TO c1 ,
         oref2 TYPE REF TO c1 .

  CREATE OBJECT : oref1 ,
                  oref2 .
  CALL METHOD oref1->m1 .
  CALL METHOD oref2->m1.

Output –>

image

Add comment

Before Buying, Compare Prices at Topprice.in

Topprice.in