posted 12 years ago
I think the answer is "it depends".
First of all, "a lot of examples" are attempting to show you something specific, trying to keep the code small, and not necessarily how to engineer it well. I'm not saying they're bad, I'm not saying they're bad examples, I'm not even saying that all of them are poor examples of software engineering, only that a lot of them are trying to illustrate something specific and not worrying about good overall program or system design.
One of the ideas of OO programming is to give you tools for dividing up your code into pieces that are then more easily maintained. So perhaps your ArrayList belongs in a class where it makes sense; if you have a class that represents a directory, then the arraylist belongs there to hold the items in the directory. It would likely be an instance variable in that case, so you get a new copy every time you instantiate the class.
Or it could be static in some class, representing, say, a list of programs running on one computer at a time. There'd likely be only one of those for a given application (likely, I said), so it would fit a static variable. I'd still likely put it in a class that represented something from the application domain, not necessarily the class that contains "main()".
And there are other considerations, other possibilities. Your arraylist could be intended for something transitory, and belong in a block within a method that is out of scope when the method returns.
There is no "one best place" to put your arraylist, just like there is no one best answer to a lot of design questions. There are better and worse questions, and there are principles to put to work to help you decide.
rc