The following code produces NullPointerException -
public class MyClass {
static MyClass instance= new MyClass(); // line 3
static Boolean have_Instance = true;
Boolean inst_Avail=have_Instance; // line 5
Boolean isInstAvail(){
return inst_Avail;
}
public static void main(String[] args) {
System.out.println(instance.isInstAvail() ? "Instance is there.":""); // gives java.lang.NullPointerException
}
}
If I move line 3 to after line 5, it runs fine. How does the order matter here? Shouldn't instantiation of a class set iVar values everytime?