• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Arraylist inside or outside main method

 
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm interested in opinions on design.

There are at least two ways to locate a Collection of commonly used data. Outside the main method were all methods can access the same Collection and change it's data. Or 2, inside main were I can make as many collections as I need and pass them as an arg to functions, objects or methods.

Which is better? My thinking tells me that 2 results in code that is easier to reuse and has better data encapsulation. But, I see a lot of examples of 1 out there which tells me 1 could be better. But, why?
 
Ranch Hand
Posts: 479
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The member variables together define the state of an object. Don't use member variables purely as an alternative to passing arguments to methods.
 
reply
    Bookmark Topic Watch Topic
  • New Topic