Why are static variables considered evil?

前端 未结 30 2733
既然无缘
既然无缘 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:49

    Static variables are not good nor evil. They represent attributes that describe the whole class and not a particular instance. If you need to have a counter for all the instances of a certain class, a static variable would be the right place to hold the value.

    Problems appear when you try to use static variables for holding instance related values.

提交回复
热议问题