I'm so confused by the difference between static variables and the global variables. When I browsing a Java textbook today, my eyes were caught by "Variables declared as static are, essentially, global variables. When an object is declared, no copy of a static variable is made." I am crystal clear about why static variable is shared by all objects in its class, but I don't get why static variables are global variables. In my understanding, the static variables could be only considered as "global" in its class.
-
1Sorry, No global variables in java.Often static variables refereed as global. – Suresh Atta Oct 21 '13 at 14:19
-
1They can be _considered_ global variables if their visibility is `public`. – Sotirios Delimanolis Oct 21 '13 at 14:19
-
No global variables in java. – Vaibhav Raj Oct 21 '13 at 14:20
-
3`Class SomeClass` with a `public static int someInt`. This `someInt` can be considered "`global`" insomuchas it can be accessed by anything that includes that class. – nhgrif Oct 21 '13 at 14:21
5 Answers
Static variables can (and should) be accessed using Class.Variable
.
A static variable will always be available globally if they're public
.
public class MyClass {
public static int X = 5;
}
Can be accessed everywhere the class is available using
MyClass.X
There's no actual 'global' keyword or anything, but it's close to its intention.

- 43,651
- 22
- 107
- 170
-
1Even if they aren't `public`, they're global state, especially if they can be manipulated via public methods. – Oct 21 '13 at 14:23
-
I think your book is (wrongly) using global as an easier way to describe a variable tied to a class.
For instance, take this class:
public class Apple {
private static int numberOfApples = 0;
public Apple() {
numberOfApples++;
System.out.println(numberOfApples);
}
}
Each time an Apple is created it will increment the numberOfApples variable and print it out. If you create two Apple objects then it will print:
1
2
The static variable in this case is globally shared by all Apple instances which may be what it means, but that is because it's tied to the class. This is different than a global variable in other languages.
edit: As others have mentioned, you can access a static variable without any instantiations of the class. If I made numberOfApples
public and printed it out before creating any Apple instances then it would print 0. Similarly, after creating two Apple classes and then having both objects be destroyed, I could print numberOfApples
and it would say 2.

- 10,440
- 8
- 52
- 79
-
The "difference to other languages" disappears if you make it **public**. – Ingo Oct 21 '13 at 14:31
-
@Ingo Good point. I think it'd be fair to say then that what's traditionally thought of as a global variable in other languages is a subset of static variables in Java because of the other possible access modifiers available? – telkins Oct 21 '13 at 15:17
-
Well ... if we take *static* to mean "neither local to functions nor dynamically allocated" and *global* to mean "can be accessed from other modules compilation units, or the like under the same fixed name" we have the same concepts in many languages. Java is IMHO absolutely un-exceptional in this regard. – Ingo Oct 21 '13 at 15:56
Static: there exists exactly one variable with that name. (while instance variables exist for every instance)
Global: Static and visibility is public.
Hence, every global variabel must be static.
Example of a global variable is: java.lang.System.out

- 36,037
- 5
- 53
- 100
What's the difference between the static variable and the global variable (Java)?
The difference is that global variables don't exist in Java. Your book shouldn't even have mentioned them.

- 305,947
- 44
- 307
- 483
As far as i Know, memory is allocated by object which is declared in main which calls the method. If it invokes a non-static variable, then it is initialized everytime it is invoked. On the other hand, memory is allocated for static variable once only then whenever it is called it's value remains same.

- 1