Hi there, I'm new to java and would appreciate help with this.
My project involves me creating 5 classes; Person, Employee, Demonstrator, Payroll and Test.
Employee and demonstrator are inherited from person. Data is read in from 2 seperate text files for each of these.
Test contains the main() method
Payroll contains an array of Person (the monthly payroll), and contains methods to add or remove employees/demonstrators from the payroll.
There are various other methods/minor details but I don't think they affect my query.
Anyway, I've spent quite a few hours at this and have figured out most of it, but there's one fundamental thing which I'm having trouble with and that's static and non-static methods. I create multiple employees and demonstrators using a for loop and array in both Employee and Demonstrator, but I'm not sure whether this should be a static method, as when I used a static method, and then used payroll with static methods it all seems to work but from what I've been reading on Google/various forum this is probably wrong.
When I use a non-static method I use a getEmployeeArra y() method which just returns the array. But I don't really know how refer to this in the payroll class. Whereas when the methods in Payroll and this one in Employee are static I can just use Employee.getEmp loyeeArray().
If anyone could shed some light it would be greatly appreciated. I'd paste all my code but I now have 3 different versions depending on which you say is the right way to go about it.
Thanks, in advance!
My project involves me creating 5 classes; Person, Employee, Demonstrator, Payroll and Test.
Employee and demonstrator are inherited from person. Data is read in from 2 seperate text files for each of these.
Test contains the main() method
Payroll contains an array of Person (the monthly payroll), and contains methods to add or remove employees/demonstrators from the payroll.
There are various other methods/minor details but I don't think they affect my query.
Anyway, I've spent quite a few hours at this and have figured out most of it, but there's one fundamental thing which I'm having trouble with and that's static and non-static methods. I create multiple employees and demonstrators using a for loop and array in both Employee and Demonstrator, but I'm not sure whether this should be a static method, as when I used a static method, and then used payroll with static methods it all seems to work but from what I've been reading on Google/various forum this is probably wrong.
When I use a non-static method I use a getEmployeeArra y() method which just returns the array. But I don't really know how refer to this in the payroll class. Whereas when the methods in Payroll and this one in Employee are static I can just use Employee.getEmp loyeeArray().
If anyone could shed some light it would be greatly appreciated. I'd paste all my code but I now have 3 different versions depending on which you say is the right way to go about it.
Thanks, in advance!
Comment