Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why are static variables considered evil?

Tags:

java

static

I am a Java programmer who is new to the corporate world. Recently I've developed an application using Groovy and Java. All through the code I wrote used quite a good number of statics. I was asked by the senior technical lot to cut down on the number of statics used. I've googled about the same, and I find that many programmers are fairly against using static variables.

I find static variables more convenient to use. And I presume that they are efficient too (please correct me if I am wrong), because if I had to make 10,000 calls to a function within a class, I would be glad to make the method static and use a straightforward Class.methodCall() on it instead of cluttering the memory with 10,000 instances of the class, right?

Moreover statics reduce the inter-dependencies on the other parts of the code. They can act as perfect state holders. Adding to this I find that statics are widely implemented in some languages like Smalltalk and Scala. So why is this opposition to statics prevalent among programmers (especially in the world of Java)?

PS: please do correct me if my assumptions about statics are wrong.

like image 423
Vamsi Emani Avatar asked Aug 11 '11 13:08

Vamsi Emani


People also ask

Why are global and static objects evil?

One reason statics might be considered evil by people is that they violate the object-oriented paradigm. In particular, it violates the principle that data is encapsulated in objects .

Are static global variables bad?

Yes, that problem will remain the same. If your code is restricted by the fact that there is only one global variable, then you lose functionality, or your code may not be thread safe, and so on. That's a problem and needs to be handled for a static variable just as for a global variable.

Are static variables inefficient?

Using static variables may make a function a tiny bit faster. However, this will cause problems if you ever want to make your program multi-threaded.

Is static variable safe?

Static variables are not thread safe. Instance variables do not require thread synchronization unless shared among threads. But, static variables are always shared by all the threads in the process. Hence, access to static variable is not thread safe.


1 Answers

Static variables represent global state. That's hard to reason about and hard to test: if I create a new instance of an object, I can reason about its new state within tests. If I use code which is using static variables, it could be in any state - and anything could be modifying it.

I could go on for quite a while, but the bigger concept to think about is that the tighter the scope of something, the easier it is to reason about. We're good at thinking about small things, but it's hard to reason about the state of a million line system if there's no modularity. This applies to all sorts of things, by the way - not just static variables.

like image 151
Jon Skeet Avatar answered Sep 27 '22 20:09

Jon Skeet