Application Development and Automation Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

Call to instance method from static method

Former Member
0 Likes
5,909

Hi

i have static method and in this method i want to call to instance method (from different class - but this two class are frind )and when i do that i get error why ?

i cant call to instance method in static method ?

Regards

Nina

1 ACCEPTED SOLUTION
Read only

MarcinPciak
Active Contributor
3,277

Depending which calss states to be friend of which.

If you do call in class 1, so class 2 has to have class 1 in his friends, not vice versa.

Example

I allow my friend riding my bike (as I consider him as a friend), but he doesn't really like me so much, so he doesn't lend me his car (as I am not considered as his friend).

Regards

Marcin

4 REPLIES 4
Read only

Former Member
0 Likes
3,277

hi,

to call instance method:

ref_obj->method_name

EXPORTING

.............

regards,

Archana

Read only

Former Member
0 Likes
3,277

Hello Nina,

You can definetly call instance method from static method. Check your code, see if proper instance of class is created with all the required parameter.

Hope you are having following steps in statuc method.

define object variable using DATA <obj> TYPE REF TO <class_name>.

create object using CREATE OBJECT syntax.

calling instance method using CALL METHOD

It should work fine.

Hope this helps!

Thanks,

Augustin.

Read only

MarcinPciak
Active Contributor
3,278

Depending which calss states to be friend of which.

If you do call in class 1, so class 2 has to have class 1 in his friends, not vice versa.

Example

I allow my friend riding my bike (as I consider him as a friend), but he doesn't really like me so much, so he doesn't lend me his car (as I am not considered as his friend).

Regards

Marcin

Read only

Former Member
0 Likes
3,277

Hi, I had tried to re-create your recuirement, but I have to give you an alternative answer to your problem.

Try to pass a generic reference from your object ( TYPE REF TO object). Inside your static method pass with wide casting the reference into a static data the reference and then make all that you want. I had write for you a simple example.

In this example you are generating 2 objects, ref_1 and ref_2.

you are calling the static method ref_2->READ_FRIEND_COUNTER to read the instance data of the object ref_1. The importing parameter is TYPE REF TO object. Inside the method you are assinging the object reference to your object from the class that you want and with this tricky way you have access into the instance attribute from the static method.

With Regards

George

-


&----


*& Report ZGMIKE_TEST

*&

&----


*&

*&

&----


REPORT ZGMIKE_TEST.

CLASS LCL_TEST_2 DEFINITION DEFERRED.

CLASS LCL_TEST_1 DEFINITION FRIENDS lcl_test_2.

PUBLIC SECTION.

METHODS: INCRES_COUNTER,

read_counter EXPORTING ex_counter TYPE i.

DATA: counter TYPE I.

ENDCLASS.

CLASS LCL_TEST_2 DEFINITION.

PUBLIC SECTION.

CLASS-METHODS: READ_FRIEND_COUNTER importing im_ref TYPE REF TO object.

PRIVATE SECTION.

DATA: test TYPE I.

CLASS-DATA: ref_friend TYPE REF TO LCL_TEST_1.

ENDCLASS.

CLASS LCL_TEST_1 IMPLEMENTATION.

METHOD INCRES_COUNTER.

ADD 1 TO counter.

ENDMETHOD.

METHOD read_counter.

ex_counter = counter.

ENDMETHOD.

ENDCLASS.

CLASS lcl_test_2 IMPLEMENTATION.

METHOD READ_FRIEND_COUNTER.

ref_friend ?= im_ref.

CALL METHOD ref_friend->INCRES_COUNTER.

ENDMETHOD.

ENDCLASS.

DATA: ref_1 TYPE REF TO lcl_test_1,

ref_2 TYPE REF TO lcl_test_2,

g_counter TYPE i.

START-OF-SELECTION.

CREATE OBJECT: ref_1, ref_2.

CALL METHOD ref_2->READ_FRIEND_COUNTER( exporting im_ref = REF_1 ).

CALL METHOD ref_1->read_counter importing ex_counter = g_counter.