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?

3
  • 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 Commented May 26, 2021 at 6:39
  • Yes, I initially declared ArrayList<Issue> and removeIssue as static , but now I need ArrayList<Issue> and removeIssue to be as non-static now. Commented May 26, 2021 at 6: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. Commented May 26, 2021 at 6:45

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

so you first add the Issue to the project, so you can then add the Project to the Issue ... very poor design

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.