Why there are no global variables in java? If I like to use any variable in all classes of a program then how can I do that?
为什么java中没有全局变量?如果我喜欢在程序的所有类中使用任何变量,那么我该怎么做呢?
11 个解决方案
#1
43
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.
但是,建议您尽量不要这样做——这样做会使代码变得不那么优雅、更难维护、更难测试。
#2
9
Global variables (in the Java context - public static
variables) are bad, because:
全局变量(在Java上下文中—公共静态变量)是不好的,因为:
-
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ško Havery的文章
-
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.
要做一个澄清,似乎需要-变量!=常量。变量会改变,这就是问题所在。因此,拥有一个公共静态的最终int DAYS_IN_WEEK = 7是完全可以的——没有人可以改变它。
#3
8
Some valid global "variables" are constants ;-) for example: Math.PI
一些有效的全局“变量”是常量;-)例如:Math.PI
#4
6
C++ is multi-paradigm, whereas Java is pure "almost exclusively" an object orientated. Object orientated means that every piece of data must be inside of an object.
c++是多范式的,而Java“几乎完全”是面向对象的。面向对象意味着每一块数据都必须位于对象的内部。
Please see the links for more information.
更多信息请参见链接。
#6
2
If you need a certain resource that is accessed from any part of your program, have a look at the Singleton Design Pattern.
如果您需要从程序的任何部分访问某个资源,请查看单例设计模式。
#7
2
I just wanted to add that if you want to use a constant global variable its safe. To use it use the final keyword. Example:
我只是想补充一点,如果你想用一个恒定的全局变量它的安全。要使用它,请使用final关键字。例子:
public static final int variable;
#8
1
Global variables do not jive well with OOP. Still one can have constants as global variables. In a sense, singleton
are global variables. If you want to have constants as global variables it is better to use enums
in stead.
全局变量不能很好地适应OOP。仍然可以用常量作为全局变量。在某种意义上,单例是全局变量。如果您想让常量作为全局变量,最好使用enum代替。
EDIT:
编辑:
Constants which used to invoked as Class.Constant
now can used Constant
by using static import
. This comes as close as possible to global variables in C++.
常量,用于作为类调用。常量现在可以通过使用静态导入来使用常量。这在c++中尽可能地接近全局变量。
#9
0
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。数+ 1;
#10
0
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.
根据定义,依赖于“全局变量”的东西不是完全可移植的,也不是完全可重用的。它依赖于在新(重用)系统上存在的全局变量,它依赖于新(重用)系统对全局变量进行操作的代码。此时,最好将全局变量放入自己的对象中。
#11
0
You can use a static class with fields synchronized and getters setters used. 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.
您可以使用一个静态类,使用同步字段和getter setter。直接更改公共静态字段是一种糟糕的做法。至少可以使用get set方法来验证对字段的访问。
Edit: If you are downvoting at least make comment so I can understand why.
编辑:如果你在投反对票,至少要发表评论,这样我才能理解为什么。
#1
43
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.
但是,建议您尽量不要这样做——这样做会使代码变得不那么优雅、更难维护、更难测试。
#2
9
Global variables (in the Java context - public static
variables) are bad, because:
全局变量(在Java上下文中—公共静态变量)是不好的,因为:
-
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ško Havery的文章
-
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.
要做一个澄清,似乎需要-变量!=常量。变量会改变,这就是问题所在。因此,拥有一个公共静态的最终int DAYS_IN_WEEK = 7是完全可以的——没有人可以改变它。
#3
8
Some valid global "variables" are constants ;-) for example: Math.PI
一些有效的全局“变量”是常量;-)例如:Math.PI
#4
6
C++ is multi-paradigm, whereas Java is pure "almost exclusively" an object orientated. Object orientated means that every piece of data must be inside of an object.
c++是多范式的,而Java“几乎完全”是面向对象的。面向对象意味着每一块数据都必须位于对象的内部。
Please see the links for more information.
更多信息请参见链接。
#5
#6
2
If you need a certain resource that is accessed from any part of your program, have a look at the Singleton Design Pattern.
如果您需要从程序的任何部分访问某个资源,请查看单例设计模式。
#7
2
I just wanted to add that if you want to use a constant global variable its safe. To use it use the final keyword. Example:
我只是想补充一点,如果你想用一个恒定的全局变量它的安全。要使用它,请使用final关键字。例子:
public static final int variable;
#8
1
Global variables do not jive well with OOP. Still one can have constants as global variables. In a sense, singleton
are global variables. If you want to have constants as global variables it is better to use enums
in stead.
全局变量不能很好地适应OOP。仍然可以用常量作为全局变量。在某种意义上,单例是全局变量。如果您想让常量作为全局变量,最好使用enum代替。
EDIT:
编辑:
Constants which used to invoked as Class.Constant
now can used Constant
by using static import
. This comes as close as possible to global variables in C++.
常量,用于作为类调用。常量现在可以通过使用静态导入来使用常量。这在c++中尽可能地接近全局变量。
#9
0
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。数+ 1;
#10
0
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.
根据定义,依赖于“全局变量”的东西不是完全可移植的,也不是完全可重用的。它依赖于在新(重用)系统上存在的全局变量,它依赖于新(重用)系统对全局变量进行操作的代码。此时,最好将全局变量放入自己的对象中。
#11
0
You can use a static class with fields synchronized and getters setters used. 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.
您可以使用一个静态类,使用同步字段和getter setter。直接更改公共静态字段是一种糟糕的做法。至少可以使用get set方法来验证对字段的访问。
Edit: If you are downvoting at least make comment so I can understand why.
编辑:如果你在投反对票,至少要发表评论,这样我才能理解为什么。