Results 1 to 4 of 4
  1. #1
    mlad is offline Member
    Join Date
    Mar 2010
    Posts
    22
    Rep Power
    0

    Default Accessing non-static fields from another class, before object creation

    Hiya guys. I am confusing myself with this problem so please bear with me in trying to explain it.

    I am making a Payroll program that will store payroll information. Currently I have a Payroll class which is static and has one field, an ArrayList of Persons, and methods to add/remove Persons and print its contents. I am adding entries to the Payroll's ArrayList, but I would like to be able to do this in a non-static context so I could have multiple, seperate Payrolls.

    My problem is that in the Person class I have a static method that reads information from a file and adds it to the ArrayList as a Person (and loops for as many Person's details are present). However, I don't know how I would go about accessing the Payroll's ArrayList from the Person class before the Payroll object has been created, if I want to make it non-static.

    Naturally, it would be easier to place this method in the Payroll class, however this is coursework and part of the specification was that it would be inside the Person class. My feeling is it's simply not possible and the professor intended Payroll to be static, but I would appreciate any information I could get.

    My code is something like this:
    Java Code:
    public class Payroll
    {
        public ArrayList<Person> payrollArray = new ArrayList<Person>();
    
        //methods
    }
    Java Code:
    public static class Person
    {
        public Person(String inName)
        {
            name = inName;
        }
    
        public static void addPersonsFromFile()
        {
            //...read data from file to newPerson
    
            Payroll.payrollArray.add(newPerson);
        }
    
        //other methods
    }
    Thanks!

  2. #2
    iluxa is offline Senior Member
    Join Date
    Mar 2010
    Posts
    266
    Rep Power
    5

    Default

    What you can do is this:

    - have a class called PayrollManager that has a static method for finding the right Payroll for the Person, that looks something like this:

    Java Code:
    class PayrollManager {
      public static Payroll findPayrollFor (Person p) {
        // if Payroll for the person doesn't exist, make a new one!
      }
    }
    In your Person class, you can then have

    Java Code:
    PayrollManager.findPayrollFor (this).addPerson (this);
    kind of thing.

  3. #3
    Steve11235's Avatar
    Steve11235 is offline Senior Member
    Join Date
    Dec 2008
    Posts
    1,046
    Rep Power
    7

    Default

    I suggest you rethink your design. In general, static fields and methods should be used only for certain patterns. Make you Payroll class hold one payroll per instance, and everything else will make more sense.

  4. #4
    mlad is offline Member
    Join Date
    Mar 2010
    Posts
    22
    Rep Power
    0

    Default

    Quote Originally Posted by Steve11235 View Post
    I suggest you rethink your design. In general, static fields and methods should be used only for certain patterns. Make you Payroll class hold one payroll per instance, and everything else will make more sense.
    It seems you've skim read my post and made a hasty, and unfortunately unhelpful reply. I've already said I want to make it non-static, and I also made it clear that I still had to be able to access the fields of Payroll from another class, which is what I was having difficulty with. You've simply reiterated my intentions.

    Iluxa's suggestion seems the best way so I will attempt to implement that. Thanks Iluxa.

Similar Threads

  1. Replies: 2
    Last Post: 03-08-2010, 10:29 AM
  2. Replies: 4
    Last Post: 10-22-2009, 05:45 PM
  3. Replies: 6
    Last Post: 03-31-2009, 04:27 AM
  4. 'Class' Object and calling Static Methods?
    By mikeiz404 in forum Advanced Java
    Replies: 3
    Last Post: 01-24-2009, 12:58 PM
  5. 'Class' Object and calling Static Methods?
    By mikeiz404 in forum New To Java
    Replies: 2
    Last Post: 01-24-2009, 05:10 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •