Three questions related to each other here. I've tried looking for answer, but I'm unable to find/apply possible existing answers to my problem. Consider the following class:
import java.util.ArrayList;
public class Container
{
private ArrayList<Box> boxList = null;
public Container()
{
this.infoList = new ArrayList<Box>();
}
public ArrayList<Box> getBoxList()
{
return this.boxList;
}
To my understanding, in order to use this class as a container for Boxes created later, I would have to construct an instance of it and then call the getter method to access the list. For example,
Container newContainer = new Container();
ArrayList<Box> list = newContainer.getBoxList();
... // Creating boxes and their contents
Box box = list.get(0); // First box in list
Item item = box.getItem(); // etc.
from where I can work with item.
Question 1: As far as I know, in order to call a private field of a class an instance of the class in question is necessary. If I insist on keeping the possibility of having multiple instances of Container, in order to access newList created above in a different class/method, do I have to keep passing it as a parameter to whatever method ever calls it (there would be many)? Of course, a method in another class does not know what newList is.
Question 2: If only one Container is ever intended to exist, would it be better to just replace
private ArrayList<Box> boxList = null;
with
private static ArrayList<Box> boxList = new ArrayList<Box>;
then remove the constructor and declare all methods of Container static, and call Container.getBoxList() directly?
Question 3: Is there a usual general setup for a class intended to only store objects and handle them (add to list, remove etc.), such as Container here? How is the communication of such class with other classes usually implemented?