简体   繁体   English

为什么java中没有像C ++那样的全局变量?

[英]Why no global variables in java like C++?

Why there are no global variables in java? 为什么java中没有全局变量? If I like to use any variable in all classes of a program then how can I do that? 如果我喜欢在程序的所有类中使用任何变量,那么我该怎么做呢?

If you really want to do that, make it a public static variable. 如果您真的想这样做,请将其设为公共静态变量。

However, you'd be advised to try not to - it makes for less elegant, harder to maintain, harder to test code. 但是,建议您不要这样做 - 它使优雅更少,维护更难,更难以测试代码。

Global variables (in the Java context - public static variables) are bad, because: 全局变量(在Java上下文中 - public static变量)很糟糕,因为:

  • harder to maintain - you can't put a breakpoint or log each change to a variable, hence unexpected values at runtime will be very hard to track and fix 难以维护 - 您无法设置断点或将每个更改记录到变量,因此运行时的意外值很难跟踪和修复

  • harder to test - read Miško Havery's post 更难测试 - 阅读MiškoHovery的帖子

  • harder to read - when someone sees the code he'll wonder: 更难阅读 - 当有人看到他会想到的代码时:

    • where does this come from? 这来自哪里?
    • where else it is read? 在哪里读它?
    • where else it is modified? 在哪里它被修改?
    • how can I know what's its current value? 我怎么知道它的当前价值是多少?
    • where is it documented? 在哪里记录?

To make one clarification that seems needed - variables != constants. 做一个似乎需要的澄清 - 变量!=常量。 Variables change, and that's the problem. 变量发生变化,这就是问题所在。 So having a public static final int DAYS_IN_WEEK = 7 is perfectly fine - no one can change it. 所以有一个public static final int DAYS_IN_WEEK = 7是完全没问题 - 没有人可以改变它。

一些有效的全局“变量”是常量;-)例如: Math.PI

C++ is multi-paradigm , whereas Java is C ++是多范式的 ,而Java则是 pure "almost exclusively" an object orientated . “几乎完全”以物体为导向 Object orientated means that every piece of data must be inside of an object. 面向对象意味着每个数据都必须在对象内部。

Please see the links for more information. 请参阅链接以获取更多信息。

为什么全局变量是邪恶的, 这里解释。

如果您需要从程序的任何部分访问某个资源,请查看Singleton设计模式

I just wanted to add that if you want to use a constant global variable its safe. 我只是想补充一点,如果你想使用一个恒定的全局变量,那么它是安全的。 To use it use the final keyword. 要使用它,请使用final关键字。 Example: 例:

public static final int variable;

Global variables do not jive well with OOP. 全局变量与OOP不相符合。 Still one can have constants as global variables. 还有一个可以将常量作为全局变量。 In a sense, singleton are global variables. 从某种意义上说, singleton是全局变量。 If you want to have constants as global variables it is better to use enums in stead. 如果要将常量作为全局变量,最好使用enums

EDIT: 编辑:

Constants which used to invoked as Class.Constant now can used Constant by using static import . 以前作为Class.Constant调用的Class.Constant现在可以通过使用static import来使用Constant This comes as close as possible to global variables in C++. 这与C ++中的全局变量尽可能接近。

package foo;
public class Globals {
  public static int count = 3;
}

Then, you can access it anywhere as 然后,您可以在任何地方访问它

int uses_global = foo.Globals.count + 1; int uses_global = foo.Globals.count + 1;

Java is intended to make fully-portable, fully-reusable objects. Java旨在制作完全可移植的,完全可重用的对象。

By definition, something that depends on a "global variable" is not fully portable or fully reusable. 根据定义,依赖于“全局变量”的东西不是完全可移植的或完全可重用的。 It depends on that global variable existing on the new (reusing) system, and it depends on code on the new (reusing) system manipulating that global variable. 它取决于新(重用)系统上存在的全局变量,它取决于操作该全局变量的新(重用)系统上的代码。 At that point, you're better off putting that global variable into an object of its own. 那时,你最好把这个全局变量放到它自己的对象中。

You can use a static class with fields synchronized and getters setters used. 您可以使用具有已同步字段和使用的getter setter的静态类。 direct changes to a public static field is a bad practice. 直接更改公共静态字段是一种不好的做法。 at least you can use get set methods for validating access to the fields. 至少你可以使用get set方法来验证对字段的访问。

Edit: If you are downvoting at least make comment so I can understand why. 编辑:如果你是downvoting至少发表评论所以我可以理解为什么。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM