3

In Java, can a static variable be anything other than a static class variable? It seems the qualifier class is not strictly necessary when referring to static variables, other than to be super clear.

Julian A.
  • 10,928
  • 16
  • 67
  • 107

4 Answers4

3

From the Java Language Specification, on fields

A static field, sometimes called a class variable, is incarnated when the class is initialized (§12.4).

They're one and the same.

Sotirios Delimanolis
  • 274,122
  • 60
  • 696
  • 724
2

There's one kind of field that can be static and isn't associated with a class: interface constants, which are both static and final (and therefore not exactly a "variable", since they don't vary).

You can use them even without initializing an implementation of the interface, so they're not necessarily associated with a class at all. I believe they're initialized when the interface is used.

Community
  • 1
  • 1
Lunivore
  • 17,277
  • 4
  • 47
  • 92
0

Static variables are class variables, in that they all live within the context of a class, but you need to be careful in that you can have a static int, or a static string, etc, as well as a static class variable.

Gwyn Evans
  • 1,361
  • 7
  • 17
0

Static fields are always associated with a class, but you don't need to write the class name if you are referencing the field from the same class.

BladeCoder
  • 12,779
  • 3
  • 59
  • 51