Java – How to make an ArrayList access a different class method?

I’m a beginner in Java and I struggle a bit. I have 3 classes and I want the ArrayList from one class to access a method from another class. How can I do that?

Here is the method I want to retrieve the method from:

  private void markAsUpdated()
  {
    this.needsUpdate = true;
  }

Here is the arrayList:

public int getNumberOfUpdatedSites()
  {
    for (int i = 0; i < webSite.size(); i++)
    {
      if (webSite.get(i))
      {

      }
    }

Both code belong to two different classes. I’m stuck at the iteration part. Basically it needs to return all the sites that are already updated. I have a UML diagram if needed I can provide it. Thanks in advance.

Answer

Assuming that Website class is as follows:

public class Website {
    private boolean needsUpdate;

    public boolean isNeedsUpdate() {
        return needsUpdate;
    }

    public void markAsUpdated() {
        this.needsUpdate = false;
    }
}

Then in your other class, you just need to call the public isNeedsUpdate method to check if it needs to be updated or not. This means that in your other class you can do as follows:

public int getNumberOfUpdatedSites() {
    int updatedWebsitesCount = 0;
    for (int i = 0; i < webSite.size(); i++) {
        if (!webSite.get(i).isNeedsUpdate()){
            updatedWebsitesCount++;
        }
    }

    return updatedWebsitesCount;
}