java 为什么Java中没有全局变量?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5581234/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 11:45:52  来源:igfitidea点击:

Why are there no global variables in Java?

javaglobal-variables

提问by developer

Why are there no global variables in Java?

为什么Java中没有全局变量?

回答by SLaks

Global variables are usually a design flaw.

全局变量通常是设计缺陷。

Your components should be self-contained and should not need any global state.
Instead, use private static fields.

您的组件应该是自包含的,并且不需要任何全局状态。
相反,使用私有静态字段。

回答by razlebe

The answer is to your question is, because Java doesn't support global variables, by design.Java was designed with object-oriented principles in mind and as such, every variable in Java is either local or a member of a class.

您的问题的答案是,因为 Java 设计上不支持全局变量。Java 的设计考虑了面向对象的原则,因此,Java 中的每个变量要么是本地变量,要么是类的成员。

Static class members are globally-accessible, which is certainly one possible definition of a global variable, depending upon your interpretation of the term. To be pedantic, whilst Static class members are accessible via the class name and therefore across multiple scopes, they are still class members; and therefore not truly global variables as such.

静态类成员是全局可访问的,这当然是全局变量的一种可能定义,这取决于您对该术语的解释。学究,虽然静态类成员可以通过类名访问,因此可以跨越多个范围,但它们仍然是类成员;因此并不是真正的全局变量。

Java's lack of support for global variables is a good thing, as using global variables is a design anti-pattern.

Java 不支持全局变量是一件好事,因为使用全局变量是一种设计反模式。

回答by kostja

Actually there is a certain similarity to global variables - you could use system properties, setting them in one class and reading them in another class. This is actually quite common for rather complex enterprise environments. JBoss for instance routinely provides a mechanism to define and access system properties.

实际上与全局变量有一定的相似性——您可以使用系统属性,将它们设置在一个类中并在另一个类中读取它们。对于相当复杂的企业环境,这实际上很常见。例如,JBoss 通常提供一种机制来定义和访问系统属性。

回答by Sangeet Menon

If its a web application that you are working in, then you can use a session variable as a global variable....which will be available in all pages in the web app...

如果它是您正在使用的 Web 应用程序,那么您可以使用会话变量作为全局变量....它将在 Web 应用程序的所有页面中可用...

回答by Imran

we can use static classe to hold Global/Application variables Eg

我们可以使用静态类来保存全局/应用程序变量,例如

//Global Class
public class GlobalClass {
            public static String globalVariable = "globalValue";
    }



//Main Method
public static void main(String args[]){

      System.out.println("globalVariable:"+GlobalClass.globalVariable);
      GlobalClass.globalVariable = "newglobalValue";
      System.out.println("globalVariable:"+GlobalClass.globalVariable);

   }

回答by mikera

The rational reasonis that global variables are usually a bad idea so there is no downside to leaving them out of the language.

合理的理由是,全局变量通常是一个坏主意,所以没有缺点让他们出来的语言。

Then there is a practical syntactic reason: everything in Java needs to be defined within the scope of a class according to the syntax. So there isn't really anywhere "global" that you could put a named variable without changing Java's scoping rules. Arguably, the closest thing that Java allows within the syntax would be a "public static" member of a class which can be used like a global variable in many ways.

然后还有一个实际的语法原因:Java 中的一切都需要根据语法在类的范围内定义。因此,在不更改 Java 的范围规则的情况下,实际上没有任何“全局”可以放置命名变量。可以说,Java 在语法中允许的最接近的东西是类的“公共静态”成员,它可以在许多方面像全局变量一样使用。

Finally there is an often forgotten principle of simplicitythat comes in many guises: "if in doubt leave it out". Or "Keep It Simple, Stupid". Or YAGNI. In language design, often less is better in terms of features. On balance, Java is remarkably well designed and has stood the test of time from this perspective.

最后,有一个经常被遗忘的简单原则,它以多种形式出现:“如果有疑问,请忽略它”。或“保持简单,愚蠢”。或YAGNI。在语言设计中,通常越少越好。总的来说,Java 的设计非常好,从这个角度来看,它经受住了时间的考验。