I am reading a code one of my friends wrote to store the account of users using arraylist. So he declared private static ArrayList = new ArrayList<>(); I understood this part, but I am not sure why he has declared it as static. That brought me up to this question : When is it a good idea to declare a data structure static?
Asked
Active
Viewed 75 times
1 Answers
1
That's a question that calls for a very delicate answer, but:
When is it a good idea to declare a data structure static?
NEVER
Static members are the root of many evils. They make the class less reusable. They make the class harder/impossible to test. They make the class non thread safe by default, which can be remedied but it's difficult to get right and can cause performance issues. They put the responsibility of managing the resource in the wrong place.
This is a very big OOD topic that's been discussed to great extents, so I'll just point out a commonly used acronym that is worth looking up: SOLID principles.