Why are there no global variables in Java?
-
6@experimentX: For certain definitions of "fully". :) – Adam Paynter Apr 07 '11 at 12:48
-
1you could circumvent that though using the GodObject pattern :) – kostja Apr 07 '11 at 12:49
-
2Too localized? You closers realize he's talking about the programming language, not the island right? – Mark Peters Apr 07 '11 at 15:38
-
3Copy of [Java IAQ - Why are there no global variables in Java?](http://norvig.com/java-iaq.html#global) by Peter Norvig. – Dori Apr 09 '11 at 06:52
6 Answers
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.

- 868,454
- 176
- 1,908
- 1,964
-
3There are some legitimate cases where you need one and Java provides them using `static` – Teja Kantamneni Apr 07 '11 at 12:49
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.

- 7,134
- 6
- 42
- 57
-
Java does, in fact, have global variables. Or how would `System.out` not be a global variable? If we go with the Wikipedia definition, then it is clear that Java *does* have global variables, hence the question cannot be answered, because it rests on faulty assumptions. – Ingo Mar 13 '13 at 09:15
-
1I've chosen to interpret the question as "Why doesn't Java have global variables" (as in, variables which are truly global in scope, as opposed to variables like System.out which are scoped to a class but globally accessible because they are static) because it's the only context in which the question makes sense. I see what you mean though, as I've quoted Wikipedia and so contradicted that position. I'll edit. – razlebe Mar 13 '13 at 10:53
-
4@Ingo Wikipedia defines 'global' as 'accessible in every scope'. `out` is only accessible within the context of `System`, so it isn't global, and `System` itself isn't a variable. Ergo `System.out` is not a global variable. Wikipedia also says Java has [no explicit globals]('http://en.wikipedia.org/wiki/Global_variable#Java:_no_explicit_globals). If it said anything else when you read it, it was wrong. – user207421 Jan 06 '14 at 02:36
-
1@EJP This is utter formalism. Either that, or confusion of scoping rules of simple names with **accessibility** of data. One can access the data named by "System.out" precisely by writing that name. Where is it said that global data must have a *simple* name? – Ingo Jan 06 '14 at 09:13
-
2@EJP The 2nd paragraph of the wiki page you cited lists the reasons we care about global variables in the first place. Note how this all applies to public static class members. The Wiki article is a bit deficient insofar it focusses on *global variables* (i.e names), which is just the usual way to feature global state, but not the only one. – Ingo Jan 06 '14 at 09:22
-
@Ingo `System.out` refers to the static field [`out`](https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/lang/System.html#out) of type [`PrintStream`](https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/io/PrintStream.html) on the [`java.lang.System`](https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/lang/System.html) class. This is exactly what this answer was referring to when it specifically called out `Static class members`. – M. Justin Oct 07 '21 at 21:58
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.

- 60,521
- 48
- 179
- 224
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...

- 9,555
- 8
- 40
- 58
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);
}

- 2,906
- 1
- 19
- 20
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.

- 105,238
- 25
- 256
- 415