开发者

Why are there no global variables in Java? [closed]

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center. Closed 11 years ago.

The community reviewed whether to reopen this question 1 year ago and left it closed:

Opinion-based Update the question so it can be answered with facts an开发者_如何学Cd citations by editing this post.

Why are there no global variables in Java?


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.


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.

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.


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.


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...


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);

   }


The rational reason is 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.

Finally there is an often forgotten principle of simplicity that 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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜