2008 Mar 21 9:48 AM
 In BADI what is the difference between Static Method and Instance Metbod?
2008 Mar 21 9:52 AM
Static Methods: these methods can be called without creating an object for the class. these methods can be directly called using the class name like
cl_gui_frontend_services=>gui_download().
Instance Methods: These methods can be only called using the object of the class.
Hope this helps.
Thanks,
Balaji
2008 Mar 21 12:46 PM
Instance & Static Method :
if u declare one method as a static then we can call that method using class name, that method is independent of that object.You declare them using the CLASS-DATA statement.
if u declare one method as a instance then we can call that method using object name, that method is dependent of that object.You declare them using the DATA statement.
Instance & Static Attribute :
if u declare one attribute as a static then we can use that attribute through class name, that attribute is independent of that object.You declare static methods using the CLASS-METHODS statement.
if u declare one attribute as a instance then we can use that attribute through object name, that attribute is dependent of that object.You declare instance methods using the METHODS statement.
2023 Jun 27 5:23 PM
2008 Mar 21 1:26 PM
Hi,
Instance components exist separately in each instance (object) of the class and are referred using instance component selector using '->'
Static components only exist once per class and are valid for all instances of the class. They are declared with the CLASS- keywords
Static components can be used without even creating an instance of the class and are referred to using static component selector => .
exapmle 1:
The program contains a class C1 with static attribute : NUM . The method : M1 increments the static attribute by 1 and displays the value each time it is called.
In the main START-OF-SELECTION portion, two objects : OBJ1 and OBJ2 are created from class C1.
First, static attribute : NUM is changed and accessed outside the class using the class component selector , =>.
Then, both objects OBJ1 and OBJ2 are used to call method : M1 which shows the new value of static attribute : NUM .
That the value of the static attribute gets incremented each time when the method M1 of different objects is called shows that this variable is able to retain its value through the entire runtime.
report ysubdel.
CLASS c1 DEFINITION .
PUBLIC SECTION.
CLASS-DATA : NUM TYPE I .
METHODS : M1.
ENDCLASS.
CLASS c1 IMPLEMENTATION.
METHOD m1 .
num = num + 1.
write:/5 num .
ENDMETHOD.
ENDCLASS.
START-OF-SELECTION.
c1=>num = 3.
write:/5 c1=>num .
DATA : OREF1 TYPE REF TO C1 ,
OREF2 TYPE REF TO C1 .
CREATE OBJECT : OREF1 ,
OREF2 .
CALL METHOD OREF1->M1 .
CALL METHOD OREF2->M1.
Example2:
Static methods of a class can only use static attributes of that class. It cannot use instance attributes. But, instance methods can use both.
Desc:
Component type static/instance
stnum Data static
Instnum Data Instance
Stmeth Method Static
Instmeth Method Instance
Both the static and instance methods are attempting to display values of the static and instance attributes: STNUM and INSTNUM .
On compilation, an error will be generated which will demonstrate that static method STMETH cannot work with instance attribute, INSTNUM.
REPORT YSUBDEL.
CLASS C1 DEFINITION.
PUBLIC SECTION.
CLASS-DATA : STNUM TYPE I VALUE 5.
DATA : INSTNUM TYPE I VALUE 6 .
CLASS-METHODS : STMETH .
METHODS : INSTMETH .
ENDCLASS.
CLASS C1 IMPLEMENTATION.
METHOD : STMETH .
WRITE:/5 STNUM .
WRITE:/5 INSTNUM .
ENDMETHOD.
METHOD INSTMETH.
WRITE:/5 STNUM .
WRITE:/5 INSTNUM .
ENDMETHOD.
ENDCLASS.
START-OF-SELECTION.
DATA : OREF1 TYPE REF TO C1.
CALL METHOD c1=>stmeth .
CREATE OBJECT OREF1.
CALL METHOD oref1->instmeth.
If it is helpful rewards points
Regards
Pratap.M
2024 Mar 25 5:55 AM - edited 2024 Mar 25 5:58 AM
Thanks for explaining the concepts in simple and easy way to understand.
Please confirm
Instance and static attribute keyword is Data and class-data respectively.
Methods of instance and static keyword is methods and class-methods respectively.
2020 Aug 19 11:48 AM
Hi,
As per my knowledge i want to share some differences below.
Instant Method or Component :
OBJ-><METHOD_NAME>
Static Method or component :
i hope its helpful for you,
Thanks and Regards,
P.kiran kumar reddy.
2020 Sep 14 3:05 PM
Hi
Difference between Instance Method and Static Method :
> Instance methods belongs to objects whereas Static Method belongs to a class.
>for Static methods (or static attributes) memory allocated only once i.e we can assign or change the values but the memory location will be same where as for Instance Method whenever we are assigning new values , different memory is allocating.
>we can't access Instance Method or Instance Attributes inside static method, whereas we can access static method inside instance methods.
>syntax for calling instance method:
DATA : OBJ TYPE REF TO (class_name).
CREATE OBJECT OBJ.
CALL METHOD OBJ->(method name).
>syntax for calling static method:
(CLASS NAME)=>(METHOD NAME).
Example:
REPORT ZVAR_CLASS2.
CLASS ABC DEFINITION.
PUBLIC SECTION.
METHODS : ADD. "INSTANCE METHOD
CLASS-METHODS : SUB. "STATIC METHOD
DATA : N1 TYPE I,
N2 TYPE I,
IOP TYPE I. "INSTANCE ATTRIBUTES
CLASS-DATA : M1 TYPE I,
M2 TYPE I,
SOP TYPE I. "STATIC ATTRIBUTES
ENDCLASS.
CLASS ABC IMPLEMENTATION.
METHOD ADD.
N1 = 25.
N2 = 50.
IOP = N1 + N2.
WRITE 😕 'OUTPUT IS:' , IOP.
ENDMETHOD.
METHOD SUB.
M1 = 105.
M2 = 95.
SOP = M1 + M2.
WRITE 😕 'OUTPUT IS:' , SOP.
ENDMETHOD.
ENDCLASS.
START-OF-SELECTION.
"CALLING STATIC METHOD.
ABC=>SUB( ).
"CALLING INSTANCE METHOD.
DATA : OBJ TYPE REF TO ABC.
CREATE OBJECT OBJ.
OBJ->ADD( ).
hope this helps
Thanks & Regards,
Varun Shukla.
2024 Jul 28 9:14 AM