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

OO Question?

Former Member
0 Likes
735

Any reason to do this in a program:

DATA: o_zcl_icg_calculations TYPE REF TO zcl_icg_calculations.

CREATE OBJECT o_zcl_icg_calculations.

CALL METHOD o_zcl_icg_calculations->calc_wettons_unit_price

EXPORTING

zunit_price_reg = unit_price

IMPORTING

z_wettons_unit_price = g_wettons_unit_price.

********************************************************************

Why not just do this....any reason?:

CALL METHOD zcl_icg_calculations->calc_wettons_unit_price

EXPORTING

zunit_price_reg = unit_price

IMPORTING

z_wettons_unit_price = g_wettons_unit_price.

1 ACCEPTED SOLUTION
Read only

RichHeilman
Developer Advocate
Developer Advocate
0 Likes
715

Hi Tom, if your question is related to .... why create an instance method over a static method, or vice-versa, it all depends on the application. In a case, where you have a simple utility method, where you pass something, it does something, and gives you a result, then there is no reason why it couldn't just be a static method. But if you are manipulating instance attributes of the class, then it should be an instance method.

So in your case, if the method implementation is just doing something with that value that you are passing and giving a result, then sure the method could be defined as a static method, the you can simply call it directly instead of creating an instance first. But again, the method must be defined as "Static"

CALL METHOD zcl_icg_calculations=>calc_wettons_unit_price
EXPORTING
zunit_price_reg = unit_price
IMPORTING
z_wettons_unit_price = g_wettons_unit_price.

Regards,

Rich Heilman

7 REPLIES 7
Read only

former_member194669
Active Contributor
0 Likes
715

Tom,

Please check the code in method "zcl_icg_calculations"

aRs

Read only

Former Member
0 Likes
715

HI,

It all depends on what you are looking for.

in the first instace you are creating a object of that class..

let us say you are doing some calculations which are stored in the class attributes.. in this case these values are specific to the object that you are accessing.

let us say you have two methods..

save_data.. to save data into a attribute of the class.

after some time you say call display_data.. if you dont have a object reference then now way you can get the attributes value

and if you feel that you just want to use the methods of a class and got nothing to do with the conent of the attributes of the class then you can do call a method by taking class reference.

normally static methods are called with class reference

Thanks

Mahesh

Read only

alejandro_bindi
Active Contributor
0 Likes
715

To do what you suggest, method should be a class one (not an instance one), thus you should use => operator instead of ->.

Read only

RichHeilman
Developer Advocate
Developer Advocate
0 Likes
716

Hi Tom, if your question is related to .... why create an instance method over a static method, or vice-versa, it all depends on the application. In a case, where you have a simple utility method, where you pass something, it does something, and gives you a result, then there is no reason why it couldn't just be a static method. But if you are manipulating instance attributes of the class, then it should be an instance method.

So in your case, if the method implementation is just doing something with that value that you are passing and giving a result, then sure the method could be defined as a static method, the you can simply call it directly instead of creating an instance first. But again, the method must be defined as "Static"

CALL METHOD zcl_icg_calculations=>calc_wettons_unit_price
EXPORTING
zunit_price_reg = unit_price
IMPORTING
z_wettons_unit_price = g_wettons_unit_price.

Regards,

Rich Heilman

Read only

0 Likes
715

Thanks Rich...I wasn't able to reward any points. Your explanation and others was very good. I never has a reason yet to use attributes. I guess the system won't let you allow points after a certain amount of time of the posting, if I could I would?

Thanks Again.

Read only

0 Likes
715

No problem, not sure why you can't, but if you want, I can award the others for there answers here(I can't award myself). If you want to award some points to the others, then just let me know. Thanks.

Regards,

Rich Heilman

Read only

0 Likes
715

Looks like I could issue points after all....Thanks again.