<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Can one combine static and instance methods? in Application Development and Automation Discussions</title>
    <link>https://community.sap.com/t5/application-development-and-automation-discussions/can-one-combine-static-and-instance-methods/m-p/2550859#M580824</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi, &lt;/P&gt;&lt;P&gt;Can one define a method so that is can be used as both a static or an instance method? &lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;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. &lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;In other words, I'm trying to accomplish both of these with one method:&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;zcl_myclass=&amp;gt;do_something_static( im_key = '1234' ).
lv_myclass_instance-&amp;gt;do_something( ).   " key in private attributes&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Why would I want to do that? &lt;/P&gt;&lt;P&gt;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.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Any input or alternative suggestions welcome.&lt;/P&gt;&lt;P&gt;Cheers, &lt;/P&gt;&lt;P&gt;Mike&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Thu, 12 Jul 2007 14:54:45 GMT</pubDate>
    <dc:creator>pokrakam</dc:creator>
    <dc:date>2007-07-12T14:54:45Z</dc:date>
    <item>
      <title>Can one combine static and instance methods?</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/can-one-combine-static-and-instance-methods/m-p/2550859#M580824</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi, &lt;/P&gt;&lt;P&gt;Can one define a method so that is can be used as both a static or an instance method? &lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;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. &lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;In other words, I'm trying to accomplish both of these with one method:&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;zcl_myclass=&amp;gt;do_something_static( im_key = '1234' ).
lv_myclass_instance-&amp;gt;do_something( ).   " key in private attributes&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Why would I want to do that? &lt;/P&gt;&lt;P&gt;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.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Any input or alternative suggestions welcome.&lt;/P&gt;&lt;P&gt;Cheers, &lt;/P&gt;&lt;P&gt;Mike&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 12 Jul 2007 14:54:45 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/can-one-combine-static-and-instance-methods/m-p/2550859#M580824</guid>
      <dc:creator>pokrakam</dc:creator>
      <dc:date>2007-07-12T14:54:45Z</dc:date>
    </item>
    <item>
      <title>Re: Can one combine static and instance methods?</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/can-one-combine-static-and-instance-methods/m-p/2550860#M580825</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;No sure if I'm following you 100%,  but you can always call a static method from a object reference.  See here.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;

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-&amp;gt;do_something( 'Instansiated' ).

call method lcl_app=&amp;gt;do_something( 'Static' ).

&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Regards,&lt;/P&gt;&lt;P&gt;Rich Heilman&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 12 Jul 2007 15:20:56 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/can-one-combine-static-and-instance-methods/m-p/2550860#M580825</guid>
      <dc:creator>RichHeilman</dc:creator>
      <dc:date>2007-07-12T15:20:56Z</dc:date>
    </item>
    <item>
      <title>Re: Can one combine static and instance methods?</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/can-one-combine-static-and-instance-methods/m-p/2550861#M580826</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;AFAIK you can't do that. &lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;To avoid redundant code (always recommendable) you can however design it like that:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;static method do_something static importing im_key ...&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;instance method do_something.&lt;/P&gt;&lt;P&gt;call method zcl_myclass=&amp;gt;do_something_static exporting im_key = me-&amp;gt;my_private_attribute.&lt;/P&gt;&lt;P&gt;endmethod.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Regards&lt;/P&gt;&lt;P&gt;Frank&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 12 Jul 2007 15:30:44 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/can-one-combine-static-and-instance-methods/m-p/2550861#M580826</guid>
      <dc:creator>Former Member</dc:creator>
      <dc:date>2007-07-12T15:30:44Z</dc:date>
    </item>
    <item>
      <title>Re: Can one combine static and instance methods?</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/can-one-combine-static-and-instance-methods/m-p/2550862#M580827</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;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. &lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Inside the method I would like to test for instantiation and using instance data if available, thus: &lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;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.&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;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&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;lv_myobj-&amp;gt;do_something( im_parm1 = lv_myobj-&amp;gt;attr1
                        im_parm2 = lv_myobj-&amp;gt;attr2
                        .....  )&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;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.&lt;/P&gt;&lt;P&gt;Cheers, &lt;/P&gt;&lt;P&gt;Mike&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 12 Jul 2007 15:58:15 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/can-one-combine-static-and-instance-methods/m-p/2550862#M580827</guid>
      <dc:creator>pokrakam</dc:creator>
      <dc:date>2007-07-12T15:58:15Z</dc:date>
    </item>
    <item>
      <title>Re: Can one combine static and instance methods?</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/can-one-combine-static-and-instance-methods/m-p/2550863#M580828</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;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.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;


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-&amp;gt;a_attribute = im_str.
      write:/ 'Do_Something - ',  im_ref-&amp;gt;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-&amp;gt;do_something(
           exporting
               im_str = 'Instansiated'
               im_ref = o_app ).

  call method lcl_app=&amp;gt;do_something(
           exporting
               im_str = 'Static' ).

&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Regards,&lt;/P&gt;&lt;P&gt;Rich Heilman&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 12 Jul 2007 17:04:05 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/can-one-combine-static-and-instance-methods/m-p/2550863#M580828</guid>
      <dc:creator>RichHeilman</dc:creator>
      <dc:date>2007-07-12T17:04:05Z</dc:date>
    </item>
    <item>
      <title>Re: Can one combine static and instance methods?</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/can-one-combine-static-and-instance-methods/m-p/2550864#M580829</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Purposes are different for Static and Instance methodology.. You can not use single method for both purpose.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;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.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Two concepts are totally different for different purposes.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;thanks,&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Chetan Shah&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 13 Jul 2007 05:14:16 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/can-one-combine-static-and-instance-methods/m-p/2550864#M580829</guid>
      <dc:creator>Former Member</dc:creator>
      <dc:date>2007-07-13T05:14:16Z</dc:date>
    </item>
    <item>
      <title>Re: Can one combine static and instance methods?</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/can-one-combine-static-and-instance-methods/m-p/2550865#M580830</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;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. &lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Cheers, &lt;/P&gt;&lt;P&gt;Mike&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 13 Jul 2007 14:00:36 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/can-one-combine-static-and-instance-methods/m-p/2550865#M580830</guid>
      <dc:creator>pokrakam</dc:creator>
      <dc:date>2007-07-13T14:00:36Z</dc:date>
    </item>
  </channel>
</rss>

