Wednesday, March 20, 2013

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.


REPORT  zsr_test NO STANDARD PAGE HEADING.

DATA gv_time TYPE sy-uzeit.
*----------------------------------------------------------------------*
*       CLASS cl_one DEFINITION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS cl_one DEFINITION.
  PUBLIC SECTION.
    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

*----------------------------------------------------------------------*
*       CLASS cl_one IMPLEMENTATION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS cl_one IMPLEMENTATION.
  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

START-OF-SELECTION.
  CALL METHOD cl_one=>cl_m_one.

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.

4 comments:

  1. hi I have one doubt regarding static methods:
    Which component of the class can access static method?
    1.Types
    2.constants
    3.instance attribute
    4.All Events.

    ReplyDelete
  2. SAP Success Factors Real Time Hands on Training in Chennai...

    Don't always Depend on Training Institute Alone and so please aware of Best Trainers too..

    http://thecreatingexperts.com/sap-successfactors-training-in-chennai/

    If You need a Best Trainer over SAP Success Factors Means??? Please ready for an DEMO From the Trainer MR.Karthick
    CONTACT:8122241286

    Both Classroom/Online Training is Available!!!!!!

    ReplyDelete


  3. Best SAP Success Factors Training Institute in Chennai
    http://thecreatingexperts.com/sap-successfactors-training-in-chennai/
    Best SAP MM Training in Chennai
    http://thecreatingexperts.com/sap-mm-training-in-chennai/
    Best SAP SD Training in Chennai
    http://thecreatingexperts.com/sap-sd-training-in-chennai/
    http://thecreatingexperts.com/sap-hr-training-in-chennai/
    Best SAP FICO Training in Chennai
    http://thecreatingexperts.com/sap-fico-training-in-chennai/
    Best SAP ABAP Training in Chennai
    http://thecreatingexperts.com/sap-abap-training-in-chennai/
    Best SAP BASIS Training in Chennai
    http://thecreatingexperts.com/sap-basis-training-in-chennai/

    If You need a Best Trainer in SAP Success Factors??? Then be ready for a DEMO From the Trainer MR.Karthick
    CONTACT:8122241286
    http://thecreatingexperts.com/sap-mm-training-in-chennai/

    Both Classroom/Online Training is Available!!!!!!

    ReplyDelete
  4. wow very well information you had shared here. This is important thing because when we are running a business first we have to make our focusing area and what we are ready for serve among the people. And really i am much inspired with this 10 steps. Surely it will be useful for craeting the better mindset among our business. Thank you and i am looking forward your more information here

    SAP HR Training in Chennai

    SAP SD Training in Chennai

    SAP Basis Training in Chennai

    ReplyDelete

Note: Only a member of this blog may post a comment.