-3

I've heard so many different opinions, and haven't really been able to find a solid answer to this question. When/why are static variables bad?

If I wanted to store names of players in a game in a List, would it be better to make it non-static than static? If so, why?

The list would be updated everytime a player leaves/joins. It would be used to show the players who else is playing and to track their play time.

  • 3
    I'm afraid this question is too broad and opinion based for the SO format. Can you try to narrow it down to a specific case, showing your current code? – Szymon Mar 19 '14 at 05:18
  • They're not 'bad', but they're usually wrong. Many bugs are fixed by removing 'static', and few by inserting it. Most times you want instance state. – user207421 Mar 19 '14 at 05:19
  • Is it a small list on a static page? Do you shuffle between various pages and load the same list in multiple places? How often do you edit it? Give us the scenario! Everything is not bad everytime – Aadi Droid Mar 19 '14 at 05:19
  • 1
    possible duplicate of [Why are static variables considered evil?](http://stackoverflow.com/questions/7026507/why-are-static-variables-considered-evil) – Radiodef Mar 19 '14 at 05:20
  • They don't allow multiple instances of content (obviously), they don't encourage encapsulation or control, they introduce referencing issues, as it's easy to suddenly have the reference changed without your knowledge (see this a lot on SO questions), they don't encourage OO or OO design principles, they stink of bad design... – MadProgrammer Mar 19 '14 at 05:21
  • refer to these links might be helpful http://stackoverflow.com/questions/12492969/static-variables-good-or-bad http://stackoverflow.com/questions/3816952/java-advantage-of-using-static-variable-in-java – Sorcrer Mar 19 '14 at 05:24
  • Use the right tools for the job, and design your software in a way that produces clear, robust, reusable, maintainable code. How (and if) `static` variables fit into this depends entirely on your situation. – Jason C Mar 19 '14 at 05:24

2 Answers2

4

Static variables aren't necessarily bad, but a major principle of software design is that you should contain information to the smallest context that needs to know it. If it really makes sense for a variable to be shared among all instances of a class--logger objects are a common example--then making it static is just fine. If it would ever make sense to have more than one version or copy of it, as it would in the case of the players of a game (think multiple games running simultaneously), then it's best to place the list in the smallest possible (non-static) context.

chrylis -cautiouslyoptimistic-
  • 75,269
  • 21
  • 115
  • 152
-1

Static variables make it more difficult to see the data as the input/output of functions, because any function has access to a static. It becomes an issue when you get thousands or tens-of-thousands lines of code. Not so much in your first small app.

It's also easier to "leak memory" because the life of a static is the same as the program, meaning it never gets garbage collected till the pogram closes, or it's nulled by code.

NameSpace
  • 10,009
  • 3
  • 39
  • 40
  • It's not a memory leak if there's still a reference to it, even if the reference is static, and even if the object is no longer needed. – Jason C Mar 19 '14 at 05:26
  • There leak is in quotes, it still a leak of sorts. its a common reason why apps run out of memory b/c they have references to what they don't need. – NameSpace Mar 19 '14 at 05:28
  • It is a more acceptable statement now that you've added quotes. – Jason C Mar 19 '14 at 05:29