banner ads

Static Method of Class

Static Method is the class method and it can access the static attributes and global attributes only. It cannot access any Instance attributes. When we declare a static method then we don't have to create any object to call that method. 
We can call a static method directly by the class where it has been declared. 

In the following example we have declared a static method in public section of a class. The method will need to access a variable which has been declared as static. The method can have the access of global variable as well. So we have declared a variable gv_time as global and a static variable cl_v_date in Public section of the class. Now the method has the access of those two variable and Write those as well.


DATA gv_time TYPE sy-uzeit.
*       CLASS cl_one DEFINITION
    DATA v_txt TYPE char40 VALUE 'SAP ABAP Object Oriented'.
    CLASS-DATA: cl_v_date TYPE sy-datum.
    CLASS-METHODS cl_m_one.
ENDCLASS.                    "cl_one DEFINITION

  METHOD cl_m_one.
    gv_time   = sy-uzeit.
    cl_v_date = sy-datum.
    WRITE: / 'Today''s date: ', cl_v_date,
           / 'Time: ', gv_time.
  ENDMETHOD.                    "cl_m_one
ENDCLASS.                    "cl_one IMPLEMENTATION


Now we shall investigate in debugging mode. Set the breakpoint at CALL METHOD. The system will enter into the method. There is no need to instantiate the class as the method is the class method / static method.

We can see here the static method doesn’t have access to the general public component v_txt. To get access of this component we need to declare a normal method. The output is as follows.

No comments