I build an android application, and i stored around 5 array lists in static variables, so is this way recommended, and if its not, what is the affect of using such way on the performance of the application?
-
It all depends on the context.Kevin Bowersox– Kevin Bowersox2014-03-17 09:50:48 +00:00Commented Mar 17, 2014 at 9:50
-
static variables are belongs to class, not to any instances. Keep this in mindAbimaran Kugathasan– Abimaran Kugathasan2014-03-17 09:51:11 +00:00Commented Mar 17, 2014 at 9:51
1 Answer
It all depends on the context. Let's say you have a Library class that contains a List<Book>. You would want the List to be an instance variable since each library should have its own books. If you made the List<Book> static each instance of library would share the same List, which would be incorrect since libraries can have different books.
But let's say you had a class FootballTeam which contains a List<Position>. Since each Football team has the same positions this class could be shared amongst all instances of FootballTeam since one team's positions will not differ from anothers.
So it really depends on what is being placed within the List. Are the values stored in the List specific to a particular instance of a class? If so, they should not be static. Are the values common across of instances of a particular type? If so they could be static.