为什么静态变量被认为是邪恶的?

生来就可爱ヽ(ⅴ<●) 提交于 2020-08-12 13:37:10

问题:

I am a Java programmer who is new to the corporate world. 我是一位Java程序员,对公司领域来说是新手。 Recently I've developed an application using Groovy and Java. 最近,我已经使用Groovy和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? 而且我认为它们也是有效的(如果我错了,请纠正我),因为如果我必须对一个类中的函数进行10,000次调用,我将很高兴使该方法静态化并使用简单的Class.methodCall() ,而不是使该类的10,000个实例混乱,对吗?

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 . 除此之外,我发现在诸如SmalltalkScala的某些语言中广泛实现了静态方法。 So why is this oppression for statics prevalent among programmers (especially in the world of Java)? 那么,为什么在程序员中(尤其是在Java世界中)普遍存在对静态的这种压迫?

PS: please do correct me if my assumptions about statics are wrong. PS:如果我对静态的假设是错误的,请纠正我。


解决方案:

参考一: https://stackoom.com/question/TTul/为什么静态变量被认为是邪恶的
参考二: https://oldbug.net/q/TTul/Why-are-static-variables-considered-evil
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!