0

In my program, I'm making use of an ArrayList of ArrayList of booleans. I need these values carried from class to class and I've been doing so by simply making it static.

But I'm getting wind that this might be in bad taste. I was thinking that the best way to do without would be to make an object (a top-level one) with that collection as the central focus, probably with some methods to accompany this.

So my questions:

  1. Is this better?
  2. How do I keep one instance of the top-level object throughout the application so my values can transfer? I'm thinking a private constructor and static methods. Or should I just go with public fields? What is the approved method?
mango
  • 5,577
  • 4
  • 29
  • 41
  • 1
    Are you using a IOC container to manage your dependencies? – Tom Styles Dec 05 '12 at 13:02
  • 1
    What is the purpose of your ArrayList? If it's a "constant" structure that is used repeatedly (without modification) in your class or if it somehow keeps track of the status of the objects of your class then it's appropriately a static. If you're using it as a parameter that is repeatedly set and reset from other classes, that's the wrong thing to do -- it should be a regular parameter. If it's global state that's used by several classes there are arguments for and against placing it in its own class, depending on the details. – Hot Licks Dec 05 '12 at 13:04
  • 1
    You might want to look at http://en.wikipedia.org/wiki/Singleton_pattern – IvanL Dec 05 '12 at 13:05
  • How about a Singleton object?? – Blacklight Dec 05 '12 at 13:06
  • @HotLicks The collection is altered in one class and then the values are retrieved and used in a couple more to follow. I can't simply make a new one in the other classes cause it wouldn't have the orginal values. Also sorry, i don't know what an IOS container is. – mango Dec 05 '12 at 13:08
  • @mango It is IOC not IOS . Added a link to the explanation in my answer. – Ajay George Dec 05 '12 at 13:16
  • If the object is being "owned" by that one class and the other classes are essentially "reading" it (even if they make modifications) then your current static is perfectly appropriate. In some cases it might be preferable to have "getters" and "setters" for elements of the array, to "protect" it, but that's a judgment call. – Hot Licks Dec 05 '12 at 13:32
  • (Evey programming practice is considered "in bad taste" by someone. There are MVC zealots, singleton zealots, OO "purity" zealots, etc, but most of them are living rather isolated lives.) – Hot Licks Dec 05 '12 at 13:35
  • @HotLicks is there no other way though? what if i said that using it like that on my system might introduce memory leaks? I dunno why but your response got me thinking, i could probably construct some sort of code string like `ttttffftt` and pass that between classes. then reconstruct my arraylist. sounds cumbersome. – mango Dec 05 '12 at 13:55
  • It's not clear how this could create a "memory leak", unless the array is somehow discarded but still referenced. When you make an object reference `static` the object (unless nulled) will persist for the remainder of the JVM life, but that is apparently what you want, no? – Hot Licks Dec 05 '12 at 16:39
  • @HotLicks i should have said this before but i'm developing for `android`. and components could be shut down at any time (lets say for ram or otherwise) they have methods to utilize just for these tasks, but usually they're usually best for less complex objects. my boolean arraylist was just an example but eventually i could try something even more complex. i think the singleton response might have been what i was looking for as it might avoid the memory leaks, but i'm not sure I like the looks of it. – mango Dec 06 '12 at 01:57
  • The singleton is a static, just separate from other classes and with a wrapper. It will persist for the life of the JVM just like a regular static. – Hot Licks Dec 06 '12 at 02:31
  • (There are two ways to make a static "go away". One is to simply store null into the static reference variable. The other is to load the class using its own user class loader, and then have the class go unreferenced so it's GCed.) – Hot Licks Dec 06 '12 at 02:33

2 Answers2

0

- If you don't want to go with static ArrayList, then you can make a class Singleton and place that ArrayList into that class.

Eg:

public class Test{

  private ArrayList<String> arr = new ArrayList<String>();

  private static Test uniqueInstance = new Test();

  private Test{}      // Make constructor Private

  public static Test getInstance(){

    return uniqueInstance;


   }

}
Kumar Vivek Mitra
  • 33,294
  • 6
  • 48
  • 75
  • @HotLicks but when you make a class with static field and static methods it acts as Singleton, but then its very difficult to predict the initialization of all those static field by the loader.. So its always better and safe to use Singleton rather than static methodology. – Kumar Vivek Mitra Dec 06 '12 at 09:17
  • Static fields are initialized when static fields are initialized. A singleton is just a degenerate case of a class with a static field and an "on demand" initializer -- nothing at all special about it, other than people think it's magical (and tend to overuse it for stuff that should be in some global instance instead). – Hot Licks Dec 06 '12 at 12:49
0

I need these values carried from class to class

How about some constructor injection ?

public class A (ArrayList<ArrayList<Boolean>> list)

The wiring can be delegated to an IOC (Inversion of Control) framework such as Spring or you can do it by hand using DIY-DI

I would stay away from Singletons because

Community
  • 1
  • 1
Ajay George
  • 11,759
  • 1
  • 40
  • 48
  • this is a good idea, but not applicable in my case. that's why i'm using static in the first place. – mango Dec 05 '12 at 13:16