0

I have a Projectclass, which contains non-static ArrayList of Issue object

public class Project{

    private ArrayList<Issue> issue_Array = new ArrayList<>(); 

    //method to remove Issue
    public void removeIssue(Issue issue_obj) {
        this.issue_Array.remove(issue_obj);
    }

}

And here's the Issue class

public class Issue {

    public void deleteThisIssue() {
          Project.removeIssue(this);     //this is where I don't know
    }

}

I was finding a way for the Issue object to remove itself from its ArrayList in another class. My approach is to call the removeIssue() method from the Project class and pass this. But then I realize I can't declare ArrayList<Issue> as static since different Project stores different ArrayList<Issue>, how am I able to remove the Issue itself from its ArrayList stored in another class?

Machavity
  • 30,841
  • 27
  • 92
  • 100
Vaporeon
  • 35
  • 4
  • removeIssue is an instance method, it has to be called through an instance of Project, yet you are calling it as if it is a static method – Stultuske May 26 '21 at 06:39
  • Yes, I initially declared ArrayList and removeIssue as static , but now I need ArrayList and removeIssue to be as non-static now. – Vaporeon May 26 '21 at 06:43
  • your removeIssue method should not be in the Issue class, it should be in the Project class, let's start with that. It makes no sense to have it in the Issue class. – Stultuske May 26 '21 at 06:45

1 Answers1

2
public class Issue {
    private Project project;
    public void deleteThisIssue() {
          this.project.removeIssue(this);     //do like this
    }
    public void setProject(Project project){
      this.project = project;
    }
}

At the place where you add issues to a project, inject issue with that project by invoking setProject() on Issue object.

user2599052
  • 1,056
  • 3
  • 12
  • 27
  • so you first add the Issue to the project, so you can then add the Project to the Issue ... very poor design – Stultuske May 26 '21 at 07:47