2014 Aug 07 9:30 PM
Hi
I am wondering which one is preferred? Static or Instance methods for calculating 'average' (e.g average sales).
My class lcl_company is having attributes name and sales. which represents name = name of the company, sales = total sales of one company under department: automobiles.
eg output:
Department: automobiles.
name = abc company
sales = 1000
name = xyz company
sales = 2000
total companies added: 2.
now i want to calculate the average sales of the companies under department automobiles.
I prefer using a static method (is it good choice), if not, why instance method. while using the static method, I noticed that i need to stick just with the defined attributes.
Please advice on calculating average.
Thank you.
2014 Aug 08 3:16 AM
Hi Pawan,
You`d better declare a another object, say 'Accountant', in this object, have a method used to calculating average, which importing parameters is a itab. In this itab, havd appended this two companies. I think no mater this calculating average method is static or instance, it`s ok, that`s depened on you how to abstract 'Accountant'.
BTW, static method has a small memory footprint than instance .
regards,
Archer
2014 Aug 08 11:27 AM
2014 Aug 08 1:46 PM
Hi archer
I did the same as you told.
i brought the two company into an it_tab which i defined as attribute.
private section.
data it_tab type table of ref to department.
my internal table is showing the records of both the companies but m face a new problem how to write this records to output.
2014 Aug 10 5:20 PM
Hi Pawan,
There are so many ways,:
One, as you said, you defined this itab as a attribute of this Object, then you need define another attribute 'AverNum' as public. Of cause, this Object have a method using to calculate the average number, passing the value to public attribute 'AverNum', then you can access this public attribute from outside.
Another way: Define this itab as a importing parameter of your average method, same time, this average method have a return parameter. Then you can call this static object method or instance object method from outside, and return your output by return parameter.
regards,
Archer