cancel
Showing results for 
Search instead for 
Did you mean: 

Instance and Static Method Concept

0 Kudos

Hi All,

Can you please able to clarify that if Instance method can access all the static and instance component then why we use differently static method??

Please share some good thought with example.

Regards

Suman Das Biswas

Accepted Solutions (0)

Answers (1)

Answers (1)

shankarsgs
Contributor
0 Kudos

Static Methods

Static methods are methods which can be called irrespective to the class instance. You can access only static attributes and static events within the Static method.

This is how you declare and call static method in ABAP:

* static method declaration

CLASS lcl_data DEFINITION.

  PUBLIC SECTION.

   CLASS-METHODS:

  get_data IMPORTING iv_date TYPE d.

ENDCLASS"lcl_data DEFINITION

*

* static method call - calling using class name

lcl_data=>get_data( '20130313' ).

*

CLASS lcl_data IMPLEMENTATION.

  METHOD get_data.

* do something

  ENDMETHOD"get_Data

ENDCLASS"lcl_data IMPLEMENTATION

 

Instance Methods

Instance methods are methods which can be ONLY called using the object reference. Instance methods can access instance attributes and instance events.

This is how you declared and call instance method in ABAP:

 

*Instance method declaration

CLASS lcl_data DEFINITION.

  PUBLIC SECTION.

   METHODS:

  get_data IMPORTING iv_date TYPE d.

ENDCLASS"lcl_data DEFINITION

*

* Instance method call - calling using the object reference

DATA: lo_data TYPE REF TO lcl_data.

CREATE OBJECT lo_data.

lo_data->get_data( '20130313' ).

*

CLASS lcl_data IMPLEMENTATION.

  METHOD get_data.

   " get data

  ENDMETHOD"get_data

ENDCLASS"lcl_data IMPLEMENTATION