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

Static Method and Instance Method which to Prefer over another , when to use.

Sijin_Chandran
Active Contributor
0 Likes
5,353

Hello All ,

I encountered a Static Method implementation in one of the already developed objects in my Organization .

I have worked upon Instance Methods only like SET_TABLE_FOR_FIRST_DISPLAY of Class CL_GUI_ALV_GRID.

Static Method is implemented in  a ZCLASS .

For calling an Instance method we have to do 3 steps i.e

a) Declare Reference Variable

b) Create Object using Constructor

c) Call Method(s) of the Object.

And for Static Method we can call it directly

ZCLASS=>Method.

Now in my scenario ,

The Method is called inside a BADI Implementation as shown below :

ZCOMMIT_BDC_QA11 is maintained in SE24 and Method BDC_MB1C is of Static type.

Now here is my doubts :

1. Why the programmer has called this method using SET HANDLER , ( any possible reasons )?

2. Is it possible to call the above method by simply writing ZCOMMIT_BDC_QA11=>BDC_MB1C .

3. When we prefer Static Method over Instance Method and Vice Versa

I have searched a lot and was not able to clarify these doubts of mine . It would be a great help if instead of Links a clear Explanation is made.

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
3,106

For your 1st question, The BDC_MB1C must be an event handler method. check in your class. please read more on Events and Methods.

To allow an event handler method to react to an event, you must determine at runtime the trigger to which it is to react. You need the following statement:

SET HANDLER

more info for set handler: http://help.sap.com/saphelp_nw70/helpdata/en/71/a8a77955bc11d194aa0000e8353423/content.htm

2. After reading the documentation, hopefully u will understand difference between methods and Events.

3. The link provide by Manu will give u the more about static and instance methods.

9 REPLIES 9
Read only

Former Member
0 Likes
3,106

Hi Sijin,

Please read the link, you will have a better idea, if you go through this link;

http://zevolving.com/2013/03/abap-static-vs-instance-method-which-to-use-when/

Thanks & Regards

Read only

Former Member
0 Likes
3,107

For your 1st question, The BDC_MB1C must be an event handler method. check in your class. please read more on Events and Methods.

To allow an event handler method to react to an event, you must determine at runtime the trigger to which it is to react. You need the following statement:

SET HANDLER

more info for set handler: http://help.sap.com/saphelp_nw70/helpdata/en/71/a8a77955bc11d194aa0000e8353423/content.htm

2. After reading the documentation, hopefully u will understand difference between methods and Events.

3. The link provide by Manu will give u the more about static and instance methods.

Read only

0 Likes
3,106

Hello Maju ,

Thanks for Explanation .

My 1st and 2nd doubt is somewhat clearer to me.

Yeah its an EVENT Handler method .

And regarding the 3rd point  , with respect to link pointed by Manu , is  Polymorphism the only point were we prefer a Instance Method over Static ?

Must say that document is really worthy one and it should be there in SCN as well.

Read only

arindam_m
Active Contributor
0 Likes
3,106

Hi,

I think the very basic way of differentiating would be when you want methods that you want to use like Utility Modules without depending on instance creation you can go for Static Methods. But when you want to use the methods only after the instance is created we do an Instance Method.

Static methods can be used as Utilities to achieve a common task across other codes that might not even need the various instance specific activities. The static methods usage can be seen in CL_ABAP_CHAR_UTILITIES. Here all methods have a fixed functionality and no instance specific task is required.

But when ever you want a have methods behaviors. Instance Methods have the flexibility to be inherited and used in case additional functionality is required. You can check the ABAP OO presentation's by Thomas Jung.

Cheers,

Arindam   

Read only

0 Likes
3,106

Thanks for your reply Arindam ,

I think the very basic way of differentiating would be when you want methods that you want to use like Utility Modules without depending on instance creation you can go for Static Methods. But when you want to use the methods only after the instance is created we do an Instance Method.

Yeah this thing is very clear from the way they are used.

Static methods can be used as Utilities to achieve a common task across other codes that might not even need the various instance specific activities. The static methods usage can be seen in CL_ABAP_CHAR_UTILITIES. Here all methods have a fixed functionality and no instance specific task is required

So , POLYMORPHISM is the only feature which is lacked by Static Methods ? As the name Static is in its name as well.

Read only

arindam_m
Active Contributor
0 Likes
3,106

Hi,

Some of the other restrictions are they can only access static attributes and trigger static events.

Cheers,

Arindam

Read only

Former Member
0 Likes
3,106

Hi,

Q1. Why the programmer has called this method using SET HANDLER , ( any possible reasons )?

Still I need to know exact scenario.

Q2. Is it possible to call the above method by simply writing ZCOMMIT_BDC_QA11=>BDC_MB1C?

Call method and set handler are totally different but still possibly can be the same for certain scenarios.

Q3. When we prefer Static Method over Instance Method and Vice-verse?

We prefer static method over instance when we don't need any details regarding an instance of a class(kinda generic one). We can use static method even before an instance is created for that class.

Apart for these, there are few links

http://wiki.scn.sap.com/wiki/display/ABAP/Object+Oriented

http://scn.sap.com/thread/793592  ----> This is more or less related to your question.

Hope these links will be helpful.

Cheers,

Dineshwar

Read only

0 Likes
3,106

Hello Dineshwar ,

That discussion was helpful one and the example of counter which was mentioned there is making the picture somewhat clearer.

only one copy of static methods be created for all static methods. They use the shared memory concept for all the static variables, methods or attributes

Whereas for a Instance method always a fresh copy is created.

Need to workout on that example.

Very thanks for it.

Read only

0 Likes
3,106

Q2. Is it possible to call the above method by simply writing ZCOMMIT_BDC_QA11=>BDC_MB1C?

Call method and set handler are totally different but still possibly can be the same for certain scenarios.

As I have mentioned that BDC_MB1C is EVENT METHOD ,

So I think its not possible to use it without SET HANDLER irrespective of any scenarios , correct me if I am wrong.