Application Development 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: 

Difference b/w static and instance method

Former Member

 In BADI what is the difference between Static Method and Instance Metbod?

7 REPLIES 7

Former Member

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

Former Member

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.

0 Kudos

Thank you for your valuable information.

Former Member

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

0 Kudos

 

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.

Hi,

As per my knowledge i want to share some differences below.

Instant Method or Component :

  • The instance Methods or components (data, attributes,events) are exits for each object of the class
  • The syntax of declaring the instance components are data, method, event, interfaces
  • In instance components you must create the object and by using these object you can accessing the components of a class.
  • While calling the instance components every time it will allocate the new memory location.
  • DATA : OBJ TYPE REF TO <CLASS_NAME>.

OBJ-><METHOD_NAME>

Static Method or component :

  • The static method or components are exist only once for all the objects of the class.
  • The syntax of declaring the static components are CLASS-DATA , CLASS-METHOD, CLASS-EVENTS
  • The static methods or components can access through the object or class
  • when you calling the static components it will one memory location, then again you are calling the static method in the same class it will referred to the same memory location(it uses the call by reference)
  • <CLASS NAME>=><METHOD_NAME>

i hope its helpful for you,

Thanks and Regards,

P.kiran kumar reddy.

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.