Im learning java and I have problem with get and set methods in other classes.
My first class is named Department and second is named Company. I would like to set number of staff in class Department and get number of staff in class Company.
Department class
public class Department {
public int staffNumber;
public Department() {
}
public void setStaffNumber(int staff) {
this.staffNumber= staff;
}
}
Company class
public class Company {
public Department staffNumber;
public Company() {
}
public Department getStaffNumber() {
return Department.staffNumber = Department.staffNumber;
}
}
Can you please help me with error message – non-static variable staffNumber cannot be referenced from a static context ?
Thank you
The problem is here:
The compiler will read
Department.staffNumberas:staffNumberis a static variable in theDepartmentclass. There your problem.In order to solve this, you should just return the instance data:
By the way,even if you have a
Department.staffNumberstatic attribute inside theDepartmentclass, the proposed linereturn Department.staffNumber = Department.staffNumber;won’t make any sense. It’s similar to this: