Friday, March 15, 2013

Static Attribute


Static attribute is declared with the statement CLASS-DATA. All the objects or instances can use the static attribute of the class. Validity of static attribute is not associated with the class itself not with the instances of the class.

Static attributes are accessed directly with the help of class name not interface name like cl_one=>cl_v_txt = 'SAP ABAP Dynpro'.
Static attributes can only be declared in Public Section. Otherwise it will fire syntax error.

Following program has a static data which is populated in a normal method. But we are not calling that method from user level. We are populating that data with some other value. Since this is static data, there is no need to create object for this. We can call directly by the class with '=>' symbol.

CLASS cl DEFINITION.
  PUBLIC SECTION.
    CLASS-DATA cl_v_text TYPE string.
    METHODS met.
ENDCLASS.

CLASS cl IMPLEMENTATION.
  METHOD met.
    cl_v_text 'Access from Class Implementation'.
  ENDMETHOD.
ENDCLASS.

START-OF-SELECTION.
  cl=>cl_v_text 'Access from User'.
  WRITE / cl=>cl_v_text.


Output:

Now we are calling the method and populate it again.

CLASS cl DEFINITION.
  PUBLIC SECTION.
    CLASS-DATA cl_v_text TYPE string.
    METHODS met.
ENDCLASS.

CLASS cl IMPLEMENTATION.
  METHOD met.
    cl_v_text 'Access from Class Implementation'.
    WRITE / cl_v_text.
  ENDMETHOD.
ENDCLASS.

START-OF-SELECTION.
  cl=>cl_v_text 'Access from User'.
  WRITE / cl=>cl_v_text.

  DATA obj TYPE REF TO cl.
  CREATE OBJECT obj.
  CALL METHOD obj->met.


Output:

We can declare the same thing under static method also. There is no need to create object to access that method.

CLASS cl DEFINITION.
  PUBLIC SECTION.
    CLASS-DATA cl_v_text TYPE string.
    CLASS-METHODS met.
ENDCLASS.

CLASS cl IMPLEMENTATION.
  METHOD met.
    cl_v_text 'Access from Class Implementation'.
    WRITE / cl_v_text.
  ENDMETHOD.
ENDCLASS.

START-OF-SELECTION.
  CALL METHOD cl=>met.

  cl=>cl_v_text 'View from User'.
  WRITE / cl=>cl_v_text.


Output:

1 comment:

Unknown said...

From My search…Creating Experts provides Best SAP Training with real time projects assistance.

Most of the modules are equipped with advance level topics which the student can learn from the basics to the advance level stage.

They also provide placement assistance in leading MNC companies across the globe according to the current requirements.

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

And these are the Best SAP training institute which provides Real Time Hands on Training…

codedion Technologies-9003085882

creating Experts-8122241286

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