Static variables default values with examples

By , last updated March 28, 2016

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.

Default Value of Static Variable

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  1.#INF, 1.#IND and #QNAN floating point numbers and errors

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.

Uses of Static Variables

  • Static variables can be very useful in many scenarios. First and most important use of static variable is to define constants. Once defined, it can be used throughout the program.
  • Static variables can also be used as counter or to keep track of the resources. For examples, a static variable keeps the track number of users (objects), we can simply use a static variable to count that for us.

Benefits of Static Variables

  • Saves memory: Constants can be defined easily by avoiding redundant declarations in every class.
  • Easy Access: Static variables can be easily accessed in every class as they behave as global variables which makes them easy to use.
Read also  How to sell well on Fiverr

Disadvantages of Static Variables

  • No encapsulation: They do not follow object oriented paradigm because it breaks encapsulation of data. As static variables are global so it is the characteristic of procedural programming paradigm.
  • Lifetime: As said above, static variables are created and initialized when a program is executed and destroyed when program is terminated or completely executed. Memory used by static variables cannot be garbage collected. So the static variable has the lifetime equal to the runtime of the program.

Comments

Be the first to comment.

Leave a Reply


You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

*