I'm having an issue with some pointers from what I can read about java. It always passes parameters as values rather than references. here
I have a project and employee class that should "share" an object but when I create an instance of activity, add it to the project list and then later add it to the employee list of activities it works. But then when I change the worktime for an employee it's only visible via the project instance where i add the worktime from. It's not visible when i then call the activity worktime from the employee object. Is there a way to "share" an object between classes e.g. pass it by reference like you can in PHP?
When I output the hashcodes of the activity objects in both classes they are also different...
Project class:
public class Project {
private List<Activity> activites = new ArrayList<Activity>();
public List<Activity> getActivities() {
return activites;
}
public void setActivities(Activity activity) {
this.activites.add(activity);
}
}
employee class:
public class Employee {
private List<Activity> activities = new ArrayList<Activity>();
public List<Activity> getActivities() {
return activities;
}
public void setActivity(Activity activity) {
activities.add(activity);
}
}
activity class:
public class Activity {
private String activityName;
private HashMap<Employee,Integer> workTime = new HashMap<Employee,Integer>();
public Activity(String activity) {
this.activityName = activity;
}
public HashMap<Employee, Integer> getWorkTime() {
return workTime;
}
public void setWorkTime(Employee e, Integer t) {
workTime.put(e, t);
}
}
An example of the issue:
public void main(String[] args) {
Activity a = new Activity('task i');
Project p = new Project();
p.setActivities(a);
Employee e = new Employee();
e.setActivity(a);
p.getActivities().get(0).setWorkTime(e,5);
System.out.println(p.getActivities().get(0).getWorkTime()); // 5
System.out.println(e.getActivities().get(0).getWorkTime()); // -> null (would like 5)
}