Home > Net >  Why can't I access the Object's properties from another class? (even when the Object was p
Why can't I access the Object's properties from another class? (even when the Object was p

Time:11-16

I hope you are able to help me.

My question is:

When passing an instance of an object that is in an arraylist, and I pass that arraylist to another class, how do I access it's atributes?

I can access the attributes before passing it to another class.

The "Main" class, passes on the data, to the "Employees" class.

Employees employees = new Employees();
employees.addEmployee("Orlando", "Silva", 111111111, "St. King's Street", 111111111, 11111111111111L, employees.getMinimumWage(), employees.getDayShift());
employees.addEmployee("Rui", "Guilherme", 111111111, "St. King's Street", 111111111, 11111111111111L, employees.getMinimumWage(), employees.getNightShift());
employees.addEmployee("Marco", "Alberto", 111111111, "St. King's Street", 111111111, 11111111111111L, employees.getMinimumWage(), employees.getNightShift());

The "Employees" class receives the data, adds it to an array, and from that array goes to the "AllStaff" class

Notice, that I have access to the atributes, in the method "addToAllStaff()"

public class Employees {
    // Atributes
    public String name;
    private String lName;
    private int nID;
    private String address;
    private int phNum;
    private long nSocialSecNum;
    private double minimumWage = 740.83;
    private double employeeWage;
    private String dayShift = "Day shift", afternoonShift = "Afternoon shift", nightShift = "Night shift";
    private String shift;

    private ArrayList<Employees> employeesArrayList = new ArrayList<Employees>();
    private AllStaff allStaff = new AllStaff();
    //---------------------
    // Constructors
    public Employees(){

    }

    public Employees(String name, String lName, int nID, String address, int phNum, long nSocialSecNum, double minimumWage, String shift){
        this.name = name;
        this.lName = lName;
        this.nID = nID;
        this.address = address;
        this.phNum = phNum;
        this.nSocialSecNum = nSocialSecNum;
        this.employeeWage = minimumWage;
        this.shift = shift;
        //----------------
        extraWage();
    }

    //---------------------

    public void addEmployee(String name, String lName, int nID, String address, int phNum, long nSocialSecNum, double minimumWage, String shift){
           Employees employee = new Employees(name, lName, nID, address, phNum, nSocialSecNum, minimumWage, shift);
           employeesArrayList.add(employee);
           addToAllStaff();
    }

    void addToAllStaff(){
        System.out.println("(Class Employees) employees size: "   employeesArrayList.size());

        for (int i = 0; i < employeesArrayList.size(); i  ){
            System.out.println("Employee names: "   employeesArrayList.get(i).getName());
            System.out.println("Employee names: "   employeesArrayList.get(i).name);
        }

        allStaff.addEmployees(employeesArrayList);
    }
}

In the class "AllStaff", is where I don't have access to the attributes

public class AllStaff {
    static ArrayList <AllStaff> employeesArrayList;

    public AllStaff(){

    }

    public void addEmployees(ArrayList listOfEmployees){
        System.out.println("List of employees size: "   listOfEmployees.size());

        for (int i = 0; i < listOfEmployees.size(); i  ){
            System.out.println("Employee names: "   listOfEmployees.get(i).getName());
            System.out.println("Employee names: "   listOfEmployees.get(i).name);
        }

        this.employeesArrayList = listOfEmployees;
    }

For the whole code, please visit this link: https://github.com/OrlandoVSilva/test-to-github/tree/master/test-to-github/src/mercado

I hope this question has everything you need. Thanks

CodePudding user response:

When using a List (or any object that can be generic), it's a good practice to specify the type of List you're dealing with. In your case, the method addEmployees(ArrayList listOfEmployees) takes in parameter an ArrayList, which could be an ArrayList of literally any object. It could be an ArrayList of Employees, Strings, Integers, whereas you just want it to be an ArrayList of Employees.

the solution is to change your ArrayList into a ArrayList<Employees> listOfEmployees

public void addEmployees(ArrayList<Employees> listOfEmployees){
    System.out.println("List of employees size: "   listOfEmployees.size());

    for (int i = 0; i < listOfEmployees.size(); i  ){
        System.out.println("Employee names: "   listOfEmployees.get(i).getName());
        //The above should work just fine :)
        System.out.println("Employee names: "   listOfEmployees.get(i).name); 
        //This one will work too as the "name" attribute is public but as you have a getName() method, you probably should make it private :D.
    }

    this.employeesArrayList = listOfEmployees;
}

To explain it quickly without too much details, when you're using a List object, you must specify the List's type by putting <> around it. If you don't, the default type is Object which is the parent of every types in java.

Also, i guess your static ArrayList <AllStaff> employeesArrayList; is meant to be your list of employees. So replacing it with static ArrayList <Employees> employeesArrayList; seems to be what you want to do :D

CodePudding user response:

You missed generics type in AllStuffs addEmployees() method argument. That is the problem. It should be like this:

public void addEmployees(ArrayList<Employees> listOfEmployees){
  • Related