‎2009 Feb 10 6:06 AM
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
‎2009 Feb 10 6:15 AM
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]
‎2009 Feb 10 6:12 AM
‎2009 Feb 10 6:15 AM
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]
‎2009 Feb 10 6:18 AM
hi,
Please it would me a lot appretiated and helpful to draft an answer rather then sending links.
‎2009 Feb 10 6:30 AM
‎2009 Feb 10 6:38 AM
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
‎2009 Feb 16 1:36 PM
Kindly let us know if you need more inputs...
if you have more issues do let me know
‎2009 Feb 16 2:17 PM
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??
‎2009 Feb 16 2:21 PM
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??
‎2009 Feb 16 2:35 PM
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.
‎2009 Feb 16 3:33 PM
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
‎2009 Feb 16 4:19 PM
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_NUMBERThe 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
‎2009 Feb 17 4:38 AM
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..
‎2009 Feb 17 5:30 AM
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
‎2009 Feb 16 1:48 PM
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