-1

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.

PJ Tang
  • 19
  • 2
  • 5

5 Answers5

5

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.

Jeroen Vannevel
  • 43,651
  • 22
  • 107
  • 170
0

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.

telkins
  • 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
0

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

Ingo
  • 36,037
  • 5
  • 53
  • 100
0

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.

user207421
  • 305,947
  • 44
  • 307
  • 483
0

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.

Neeraj
  • 1