I’m developing an app that has a DataManager class, which holds an ArrayList<Object[]>. As this ArrayList needs to be used within other classes, I am wondering what would be the most efficient and fastest way of accessing this list, considering this application will be running on the Android platform.
A) create a public static ArrayList<Object[]> data in the DataManager class and reference it within other classes through DataManager.data
B) create a public ArrayList<Object[]> getData method within the DataManager class and have methods within other classes create local variable ArrayList<Object[]> data = mDataManager.getData() for temporary use.
C) ..?
It seems to me B has more overhead due to object creation. Also I read static is faster than non-static?
Option B does not increase memory use, since you will only have one
ArrayListobject (all the objects that use it just hold a simple reference, not a copy). The objects that use theArrayListcould also store this reference as an instance variable, instead of requesting it from the manager class each time it is needed.I read somewhere that access to instance variables is slightly faster than accessing class (
static) variables, but I don’t have the link to the source.The difference in performance is not likely to be meaningful. However, Option B gives you better encapsulation.