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

instance method vs static method

Former Member
0 Likes
6,430

Moved to correct forum by moderator

hi ppl,

I have some simple doubts on ooabap-

what is the advantage of using -

static methods over instance methods

static data over data

and which one would be a better to use in architectural and technical point of view.

Edited by: Matt on Feb 16, 2009 4:33 PM

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
4,902

hi,

Instance methods gets reinitialized every time the we create the instance of the class.

Static methods are called only once when the program is started and they don't get reinitialized through out the execution of program.

[http://saphelp.border-states.com/EN/c3/225b5c54f411d194a60000e8353423/frameset.htm]

14 REPLIES 14
Read only

viquar_iqbal
Active Contributor
0 Likes
4,902

This message was moderated.

Read only

Former Member
0 Likes
4,903

hi,

Instance methods gets reinitialized every time the we create the instance of the class.

Static methods are called only once when the program is started and they don't get reinitialized through out the execution of program.

[http://saphelp.border-states.com/EN/c3/225b5c54f411d194a60000e8353423/frameset.htm]

Read only

Former Member
0 Likes
4,902

hi,

Please it would me a lot appretiated and helpful to draft an answer rather then sending links.

Read only

0 Likes
4,902

hi sarves, could you please elobrate on that..

Read only

0 Likes
4,902

for calling a instance method and attribute ...you have to first create intance for the class and assign reference :

ref type refrernce to cl_class.

then you call the method:

call method ref->method name

so if u r using instance method and class you have to always worry about the instance of claa ....

but in static attribute and method ...you di=ont have to worry abot the instance of class....

simply if you want to call a method which is static of class cl_class do:

call method cl_class=>method name

Read only

0 Likes
4,902

Kindly let us know if you need more inputs...

if you have more issues do let me know

Read only

0 Likes
4,902

hi,

Iam still not yet convinced..because one of my friend told that its good to use static methods compare to intstance method.even i heard that it saves memory in run time.isit so??

Read only

0 Likes
4,902

hi,

Iam still not yet convinced..because one of my friend told that its good to use static methods compare to intstance method.even i heard that it saves memory in run time.isit so??

Read only

0 Likes
4,902

for static one memory is assigned ..but for insatnce different memory is assisgned...

Instance Methods

You declare instance methods using the METHODS statement. They can access all of the attributes of a class, and can trigger all of the events of the class.

Static Methods

You declare static methods using the CLASS-METHODS statement. They can only access static attributes and trigger static events.

Read only

matt
Active Contributor
0 Likes
4,902

You use instances when it's appropriate to your program design, and you use static when that's appropriate. Arguments about which is more "efficient" and which make best use of resource are really rather moot. The argument you should be interested in is "which is better design". And very generally, usually it is better to use instance rather than static. I use statics only for information which is useful to share between instances, and for utility methods that aren't really specific to an instance.

matt

Read only

4,902

Hello Priyank

If you are programming at a level where you need to consider memory aspects then you are either an absolute high-end ABAP guru or a greenhorn.

My experience during the last ten years is that I never came across a memory problem and only once I had a severe performance problem which I solved with a little help from Rob Burbank:

[Performance of Nested Loops|https://www.sdn.sap.com/irj/scn/weblogs?blog=/pub/wlg/4725] [original link is broken] [original link is broken] [original link is broken];

I would like to extend what Matt already pointed out:

- Instance Method: depends on the specific context of an instance

- Static Method: is independent of any specific context and therefore requires no instance

Now let's assume we have a PERSON class which should have the following methods:

- GET_SOCIAL_SECURITY_NUMBER
- VALIDATE_SOCIAL_SECURITY_NUMBER

The social security number is a specific attribute of a person and therefore this method should be an instance method of this sample class.

Usually such numbers are generated according to a given algorithm which, in turn, can be used to validate a given number. Since this algorithm is intended to deal with ANY input the method should be defined as static.

CONCLUSION: Instance vs. static method means context-specific vs. context-independent.

Regards

Uwe

Read only

0 Likes
4,902

hi uwe,

Iam just clearing my doubts thats it.iam not an abap guru,iam just a kid in front of you gurus;).iam in entry level in ooabap.anyways thanks for the response guys..

Read only

4,902

Hello Priyank

To be absolutely clear: I did not mean to offend anybody but the point is that you should forget about memory in the ABAP context - unless you are really urged to consider it.

And indeed I recently had a "memory" issue:

One of our companies wants to collect huge amounts of Idocs (up to 50'000) into a single flat file. For each collected Idoc the system creates a lock entry which resulted in an overflow dump at about 3600 IDocs. We increased the corresponding profile parameter (RZ11) by the factor 15 and never had any problems since then.

Regards

Uwe

Read only

Former Member
0 Likes
4,902

Hi Priyank,

Kindly go through following.

It will help you to clear some your doubts.

http://help.sap.com/saphelp_nw04/helpdata/en/18/773e15778211d1b4510000e8a52bed/frameset.htm

Even don't hasitate to search on SCN / SAP Help

Regards,

Pratik Vora