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

Can one combine static and instance methods?

pokrakam
Active Contributor
0 Likes
955

Hi,

Can one define a method so that is can be used as both a static or an instance method?

Basically I'm trying to simplify my class to so that I can call a method either statically with parameters or instantiated using it's own attributes.

In other words, I'm trying to accomplish both of these with one method:

zcl_myclass=>do_something_static( im_key = '1234' ).
lv_myclass_instance->do_something( ).   " key in private attributes

Why would I want to do that?

I would like to avoid instantiation in some cases for performance reasons and would like to keep it simple by having only one method to do basically the same thing.

Any input or alternative suggestions welcome.

Cheers,

Mike

1 ACCEPTED SOLUTION
Read only

RichHeilman
Developer Advocate
Developer Advocate
0 Likes
894

No sure if I'm following you 100%, but you can always call a static method from a object reference. See here.



report zrich_0001.

*---------------------------------------------------------------------*
*       CLASS lcl_app DEFINITION
*---------------------------------------------------------------------*
*       ........                                                      *
*---------------------------------------------------------------------*
class lcl_app definition.

  public section.

    class-methods: do_something importing im_str type string.

endclass.

*---------------------------------------------------------------------*
*       CLASS lcl_app IMPLEMENTATION
*---------------------------------------------------------------------*
*       ........                                                      *
*---------------------------------------------------------------------*
class lcl_app implementation.

  method do_something.
    write:/ 'Do_Something - ', im_str.
  endmethod.

endclass.

data: o_app type ref to lcl_app.

start-of-selection.

create object o_app.
call method o_app->do_something( 'Instansiated' ).

call method lcl_app=>do_something( 'Static' ).

Regards,

Rich Heilman

6 REPLIES 6
Read only

RichHeilman
Developer Advocate
Developer Advocate
0 Likes
895

No sure if I'm following you 100%, but you can always call a static method from a object reference. See here.



report zrich_0001.

*---------------------------------------------------------------------*
*       CLASS lcl_app DEFINITION
*---------------------------------------------------------------------*
*       ........                                                      *
*---------------------------------------------------------------------*
class lcl_app definition.

  public section.

    class-methods: do_something importing im_str type string.

endclass.

*---------------------------------------------------------------------*
*       CLASS lcl_app IMPLEMENTATION
*---------------------------------------------------------------------*
*       ........                                                      *
*---------------------------------------------------------------------*
class lcl_app implementation.

  method do_something.
    write:/ 'Do_Something - ', im_str.
  endmethod.

endclass.

data: o_app type ref to lcl_app.

start-of-selection.

create object o_app.
call method o_app->do_something( 'Instansiated' ).

call method lcl_app=>do_something( 'Static' ).

Regards,

Rich Heilman

Read only

Former Member
0 Likes
894

AFAIK you can't do that.

To avoid redundant code (always recommendable) you can however design it like that:

static method do_something static importing im_key ...

instance method do_something.

call method zcl_myclass=>do_something_static exporting im_key = me->my_private_attribute.

endmethod.

Regards

Frank

Read only

pokrakam
Active Contributor
0 Likes
894

Thanks for the input. The problem is I cannot use instance data if it is declared as static. What may also be relevant is that this is a public class using the class builder.

Inside the method I would like to test for instantiation and using instance data if available, thus:

method do_something. 
  if im_param is supplied.
  "  use parameter data
  elseif me is bound.  " wishful thinking...
  "  use instance data
  else.  raise exception.  endif.
" do stuff
endmethod.

I was just trying to avoid the rather clumsy look of having two methods doing the same thing (actually several of these in several classes). Or the equally clumsy-looking

lv_myobj->do_something( im_parm1 = lv_myobj->attr1
                        im_parm2 = lv_myobj->attr2
                        .....  )

So far I'll probably stick with a static and pass all the instance data through for each call, but will leave the question open for a little while to see if any other suggestions surface.

Cheers,

Mike

Read only

0 Likes
894

Ok, I may be reaching here a bit, I know, but maybe this may give you some ideas. After creating the object, pass it back to the method call.




report zrich_0001.

*---------------------------------------------------------------------*
*       CLASS lcl_app DEFINITION
*---------------------------------------------------------------------*
*       ........                                                      *
*---------------------------------------------------------------------*
class lcl_app definition.

  public section.

    data: a_attribute type string.

    class-methods: do_something importing im_str type string
                               im_ref type ref to lcl_app optional.

endclass.

*---------------------------------------------------------------------*
*       CLASS lcl_app IMPLEMENTATION
*---------------------------------------------------------------------*
*       ........                                                      *
*---------------------------------------------------------------------*
class lcl_app implementation.

  method do_something.
    if not im_ref is initial.
       im_ref->a_attribute = im_str.
      write:/ 'Do_Something - ',  im_ref->a_attribute.
    else.
      write:/ 'Do_Something - ',  im_str.
    endif.
  endmethod.

endclass.

data: o_app type ref to lcl_app.

start-of-selection.

  create object o_app.
  call method o_app->do_something(
           exporting
               im_str = 'Instansiated'
               im_ref = o_app ).

  call method lcl_app=>do_something(
           exporting
               im_str = 'Static' ).

Regards,

Rich Heilman

Read only

Former Member
0 Likes
894

Hi

Purposes are different for Static and Instance methodology.. You can not use single method for both purpose.

You can have static data and static method, which can be access with an instance, but it can not be different for different instances, like instance methods and instance data.

Two concepts are totally different for different purposes.

thanks,

Chetan Shah

Read only

pokrakam
Active Contributor
0 Likes
894

Thanks for the input, at least it provided some food for thought. Though hoping for a more elegant solution, I ended up just passing all the data in anyway and treating it as a pure static.

Cheers,

Mike