Professional Documents
Culture Documents
ABAPStaticvsInstancemethodWhichtousewhen?ABAPHelpBlog
ZEVOLVING
Home
ABAP Objects
OO Concepts
17,316 | 14
WealldebateoverwhentouseStaticmethodsorInstancemethods.Mostofthetimeswegoforsimplestapproach,butthatmaynotbethecorrectone.Lets
trytoexploreandseewhatshouldbepossiblythebestapproachwhendecidingStaticorInstance.
Basics
Beforejumpingintothedifferenceandwhichshouldbeused,checkoutthebasicsofbothStaticmethodandInstancemethod.
Current Poll
DoyoulikeQualityAssuranceofyour
Build/Design?
YES. I live for it!
I don't mind
NO. I hate it
Vote
View Results
Static Methods
Staticmethodsaremethodswhichcanbecalledirrespectivetotheclassinstance.YoucanaccessonlystaticattributesandstaticeventswithintheStatic
method.
Thisishowyoudeclareandcallstaticmethod:
*staticmethoddeclaration
CLASSlcl_dataDEFINITION.
PUBLICSECTION.
CLASSMETHODS:
get_dataIMPORTINGiv_dateTYPEd.
ENDCLASS."lcl_dataDEFINITION
*
*staticmethodcallcallingusingclassname
lcl_data=>get_data('20130313').
*
http://zevolving.com/2013/03/abapstaticvsinstancemethodwhichtousewhen/
1/6
08/12/2014
ABAPStaticvsInstancemethodWhichtousewhen?ABAPHelpBlog
CLASSlcl_dataIMPLEMENTATION.
METHODget_data.
*dosomething
ENDMETHOD."get_Data
ENDCLASS."lcl_dataIMPLEMENTATION
Instance Methods
InstancemethodsaremethodswhichcanbeONLYcalledusingtheobjectreference.Instancemethodscanaccessinstanceattributesandinstanceevents.
Thisishowyoudeclaredandcallinstancemethod:
*Instancemethoddeclaration
CLASSlcl_dataDEFINITION.
PUBLICSECTION.
METHODS:
get_dataIMPORTINGiv_dateTYPEd.
ENDCLASS."lcl_dataDEFINITION
*
*Instancemethodcallcallingusingtheobjectreference
DATA:lo_dataTYPEREFTOlcl_data.
CREATEOBJECTlo_data.
lo_data>get_data('20130313').
*
CLASSlcl_dataIMPLEMENTATION.
METHODget_data.
"getdata
ENDMETHOD."get_data
ENDCLASS."lcl_dataIMPLEMENTATION
Test Fixture
InABAPunit,youcansetthetestdatainspecialmethodscalledtestfixtures.Afterthismethod,yourtestmethodwouldbecalledwhereyouhaveaccessto
testdata.SinceeachABAPUnittestshouldbeoperableandtestableonitsown,Staticmethodsmakesitverydifficulttotestwith.Staticmethodswoulduse
staticattributesandsincetheyareusingthat,youhavetohaveadditionallogictogetridofthemallthetimeinyourtestfixturemethods.
Ifyouareworkingwiththeinstanceiftheobject,itcanbeeasilycleared.Whenyouinstantiateanewobject,theoldobjectwouldbedereferencedwithoutany
additionallogic
Constructor
DesignusingthestaticmethodswouldendupusingtheCLASS_CONSTRUCTOR,asopposedtothemethodCONSTRUCTORforInstancemethods.AsI
http://zevolving.com/2013/03/abapstaticvsinstancemethodwhichtousewhen/
2/6
08/12/2014
ABAPStaticvsInstancemethodWhichtousewhen?ABAPHelpBlog
Thumb Rules
Sobasedonallthesefacts,wecanconcludetothesethumbrules:
Ifyouareplanningtocreateanystaticattributewhichwouldbeusedbystaticmethod,Considercreatinginstancemethods.Itwouldallowyoutoworkwith
multipleinstances.Italsoallowsyoutocontrolonwhenyoucanfreeuptheboundmemory.
Ifyouthinkthattherewouldbeachancetoaddaconditionallogicinfuture,Goforinstance.Thismakesdesignmoreflexiblebyallowingyoutoleverage
polymorphismby Redefinition
Staticshouldonlyusedforobjectcreationdesignpatternslike Singleton , FactoryMethod , AbstractFactory , SingletonFactory tofacilitatetheobject
creation.
Staticshouldbeforpureutilityclassesnotforhelperclasses.ThebestexampleswouldbemethodswithintheclassCL_GUI_FRONTEND_SERVICES.
LetmeknowifyouwanttoaddanyotherperspectiveofusingvsnotusingtheStaticmethod.
TOP
On a side note
ImplementedquiteafewchangesonsiteHomepageandotherpagesincludingNavigation,Menu,RelatedPostetc.Thesiteisalsonowmobile(iPhone,
Android,Nokia,andothers)aswellastablet(iPad,Galaxyandothers)friendly.Checkitoutandletusknowyourfeedback.
Naimesh Patel
{245 articles}
I'mSAPABAPConsultantformorethanadecade.IliketoexperimentwithABAPespeciallyOO.IhavebeenSDNTopContributor.
Follow:
Exploreallofhis245articles.
14 Comments
Wouter
http://zevolving.com/2013/03/abapstaticvsinstancemethodwhichtousewhen/
3/6
08/12/2014
ABAPStaticvsInstancemethodWhichtousewhen?ABAPHelpBlog
Whatdoyouguysthink?
steve oldner
Naimesh Patel
Saurabh Tiwari
Fred Verheul
Naimesh Patel
Wouter
http://zevolving.com/2013/03/abapstaticvsinstancemethodwhichtousewhen/
4/6
08/12/2014
ABAPStaticvsInstancemethodWhichtousewhen?ABAPHelpBlog
Greets,
Wouter
steve oldner
Naimesh Patel
Naimesh Patel
Wouter Peeters
http://zevolving.com/2013/03/abapstaticvsinstancemethodwhichtousewhen/
5/6
08/12/2014
ABAPStaticvsInstancemethodWhichtousewhen?ABAPHelpBlog
Naimesh Patel
Aasim
Naimesh Patel
CommentsonthisPostarenowclosed.Ifyouhavesomethingimportanttoshare,youcanalwayscontactme.
Since2008|COPYRIGHT2013zevolving|www.zevolving.com|Advertise
Allproductnamesaretrademarksoftheirrespectivecompanies.zevolving.comisnotaffiliatedwithSAPAG.
http://zevolving.com/2013/03/abapstaticvsinstancemethodwhichtousewhen/
6/6