Monday, December 26, 2005

Static Initializer in Java

Let me start with a small question. (At least it was interesting to me). Go through the following lines of code.
/* Initializer.java */
package staticinit;
/**
* A simple class to demonstrate the working of static block of code
* that is used to initialize the static class variables.
* @author prasad
*/

public class Initializer {
/** Initialize the class variables. */
public Initializer() {
firstClassVar = "VALUE_NOT_NULL";
secondClassVar = "VALUE_NOT_NULL";
thirdClassVar = "VALUE_NOT_NULL";
fourthClassVar = "VALUE_NOT_NULL";
}
/** Initialize the static variables. */
static {
firstStaticVar = "STATIC_NOT_NULL";
secondStaticVar = "STATIC_NOT_NULL";
}
/** Static variables. */
private static String firstStaticVar = null;
private static String secondStaticVar;
/** Class variables. */
private String firstClassVar = firstStaticVar;
private String secondClassVar = secondStaticVar;
private String thirdClassVar = null;
private String fourthClassVar;
/** Examine and display the values. */
public void display() {
System.out.println("firstStaticVar -> " + getValue(firstStaticVar));
System.out.println("secondStaticVar -> " + getValue(secondStaticVar));

System.out.println("firstClasssVar -> " + getValue(firstClassVar));
System.out.println("secondClassVar -> " + getValue(secondClassVar));
System.out.println("thirdClassVar -> " + getValue(thirdClassVar));
System.out.println("fourthClassVar -> " + getValue(fourthClassVar));
}
private String getValue(Object value) {
return (value == null)? "is null": value.toString();
}
/** Main entry point. */
public static void main(String[] args) {
Initializer initializer = new Initializer();
initializer.display();
}
}


Which of the variable(s) will have the value "null" in the function display()?
a) firstStaticVar
b) secondStaticVar
c) firstClassVar / secondClassVar / thridClassVar / fourthClassVar
d) none of the above

You are right if you had selected the option (a).

Why only firstStaticVar is "null"?

When the class is instantiated, before constructor is called the variables are defined (which takes the default value) and then the static initialization are performed inorder. The static initialization can be static block of code or inline assignment etc. In the above code, after static block of code is executed, firstStaticVar is re-assigned by the "inline assignment". Next secondStaticVar has no "inline assignment" so the previous initialized value is retained. Next all firstClassVar and thridClassVar are inline initialized. After this, the constructor is called which will re-initialize all the ClassVar's. When the constructor finishes its work only firstStaticVar will have the value "null".

Things like this can result in bugs which would be hard to figure out or debug. A simple solution would be to declare variables (with or without initialization) before any "code block" that access it.

Comments: Post a Comment

<< Home

This page is powered by Blogger. Isn't yours?