Why are static variables considered evil?

前端 未结 30 2565
既然无缘
既然无缘 2020-11-21 05:04

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 numbe

相关标签:
30条回答
  • 2020-11-21 05:54

    There are two main questions in your post.

    First, about static variables. Static variables are completelly unnecesary and it's use can be avoided easily. In OOP languajes in general, and in Java particularlly, function parameters are pased by reference, this is to say, if you pass an object to a funciont, you are passing a pointer to the object, so you dont need to define static variables since you can pass a pointer to the object to any scope that needs this information. Even if this implies that yo will fill your memory with pointers, this will not necesary represent a poor performance because actual memory pagging systems are optimized to handle with this, and they will maintain in memory the pages referenced by the pointers you passed to the new scope; usage of static variables may cause the system to load the memory page where they are stored when they need to be accessed (this will happen if the page has not been accesed in a long time). A good practice is to put all that static stuf together in some little "configuration clases", this will ensure the system puts it all in the same memory page.

    Second, about static methods. Static methods are not so bad, but they can quickly reduce performance. For example, think about a method that compares two objects of a class and returns a value indicating which of the objects is bigger (tipical comparison method) this method can be static or not, but when invoking it the non static form will be more eficient since it will have to solve only two references (one for each object) face to the three references that will have to solve the static version of the same method (one for the class plus two, one for each object). But as I say, this is not so bad, if we take a look at the Math class, we can find a lot of math functions defined as static methods. This is really more eficient than putting all these methods in the class defining the numbers, because most of them are rarelly used and including all of them in the number class will cause the class to be very complex and consume a lot of resources unnecesarilly.

    In concluson: Avoid the use of static variables and find the correct performance equilibrium when dealing with static or non static methods.

    PS: Sorry for my english.

    0 讨论(0)
  • 2020-11-21 05:55

    Since no one* has mentioned it: concurrency. Static variables can surprise you if you have multiple threads reading and writing to the static variable. This is common in web applications (e.g., ASP.NET) and it can cause some rather maddening bugs. For example, if you have a static variable that is updated by a page, and the page is requested by two people at "nearly the same time", one user may get the result expected by the other user, or worse.

    statics reduce the inter-dependencies on the other parts of the code. They can act as perfect state holders

    I hope you're prepared to use locks and deal with contention.

    *Actually, Preet Sangha mentioned it.

    0 讨论(0)
  • 2020-11-21 05:56

    Summarising few basic Advantages & Disadvantages of using Static methods in Java:

    Advantages:

    1. Globally accessible i.e. not tied with any particular object instance.
    2. One instance per JVM.
    3. Can be accessed by using class name (No object require).
    4. Contains a single value applicable to all instances.
    5. Load up on JVM startup and dies when JVM shuts down.
    6. They doesn't modify state of Object.

    Disadvantages:

    1. Static members are always part of memory whether they are in use or not.
    2. You can not control creation and destruction of static variable. Usefully they have been created at program loading and destroyed when program unload (or when JVM shuts down).
    3. You can make statics thread safe using synchronize but you need some extra efforts.
    4. If one thread change value of a static variable that can possibly break functionality of other threads.
    5. You must know “static“ before using it.
    6. You cannot override static methods.
    7. Serialization doesn't work well with them.
    8. They don't participate in runtime polymorphism.
    9. There is a memory issue (to some extent but not much I guess) if a large number of static variables/methods are used. Because they will not be Garbage Collected until program ends.
    10. Static methods are hard to test too.
    0 讨论(0)
  • 2020-11-21 05:56

    There's nothing wrong with static variables per se. It's just the Java syntax that's broken. Each Java class actually defines two structures- a singleton object which encapsulates static variables, and an instance. Defining both in the same source block is pure evil, and results in a code that's hard to read. Scala did that right.

    0 讨论(0)
  • 2020-11-21 05:56

    everything (can:) have its purpose, if you have bunch of threads that needs to share/cache data and also all accessible memory (so you dont split into contexts within one JVM) the static is best choice

    -> of course you can force just one instance, but why?
    i find some of the comments in this thread evil, not the statics ;)

    0 讨论(0)
  • 2020-11-21 05:56

    All the answers above show why statics are bad. The reason they are evil is because it gives the false impression that you are writing object oriented code, when in fact you are not. That is just plain evil.

    0 讨论(0)
提交回复
热议问题