In Java, static variables are also known as class variables because static variables are available through classes and not instances (objects).
When a program is executed, all static variables are initialized. They are initialized even before the initialization of any other instance variables. Once a static variable is created, its single copy is shared between all classes. It is accessed with Classname.staticVariableName
.
Static variables are mainly used as constants but they have other uses as well. Static variable can be used as final
(constant) and static
. Static and final are also known as Non Access Modifiers which means finals are used to define constants while static are those values which remain same for all classes.
Standard default values are used to initialize static variables if we doesn’t explicitly set a value for the static variable. A default initialization class is produced internally to initialize all static variables when a program is executed for the first time.
The rule of thumb is that primitives get default value as 0
and all Objects get null
.
Following table shows the variable types and how they are initialized by default.
Data Types | Default Values |
---|---|
int,short,byte,long | 0 |
char | \u0000 (space character or blank) |
boolean | false |
double/float | 0.0 |
Any reference or other types | Null |
An example JUnit test to validate the theory. In this example we are testing simple static variables and their assignment to each other:
private static int a; private static int aa = 10; private static int ab = aa; private static int ac = ab; private static int ad = ac; private static int ba = bb; // Will not compile private static int bb = 20; private static int bc = bb; private static int bd = bb; private static Integer c; private static String d; private static long e; private static Long f; private static double g; private static Double h; private static char i; private static boolean j; @Test public void testDefaultValues() { System.out.println("a:" + a); System.out.println("aa:" + aa); System.out.println("ab:" + ab); System.out.println("ac:" + ac); System.out.println("ad:" + ad); System.out.println("bb:" + bb); System.out.println("bc:" + bc); System.out.println("bd:" + bd); System.out.println("c:" + c); System.out.println("d:" + d); System.out.println("e:" + e); System.out.println("f:" + f); System.out.println("g:" + g); System.out.println("h:" + h); System.out.println("i:" + i); System.out.println("j:" + j); }
The output:
a:0 aa:10 ab:10 ac:10 ad:10 bb:20 bc:20 bd:20 c:null d:null e:0 f:null g:0.0 h:null i: j:false
As wee see, you can assign static variable values to other static variables, but you need to be careful and follow the order.
Read also: Java conversions and 7 Golden rules of widening, boxing & varargs.
Now look what happens when we make these same static variables static final
.
private static final int a; // Will not compile private static final int aa = 10; private static final int ab = aa; private static final int ac = ab; private static final int ad = ac; private static final int ba = bb; // Will not compile private static final int bb = 20; private static final int bc = bb; private static final int bd = bb; private static final Integer c; // Will not compile private static final String d; // Will not compile private static final long e; // Will not compile private static final Long f; // Will not compile private static final double g; // Will not compile private static final Double h; // Will not compile
The output:
output: aa:10 ab:10 ac:10 ad:10 bb:20 bc:20 bd:20
The are a lot more restrictions to variables that are both final and static. They become constants that must be initialized before the program starts and they can’t be modified.
Check out more information regarding why some of the scenarios won’t compile at Oracle Doc.