Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When Initializing object, instance variable not initialized always?

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?

like image 427
Naman Avatar asked Mar 06 '26 12:03

Naman


2 Answers

When an object is created on line 3 the class has not finished initializing yet, and the have_instance variable has its default value, null. This value is assigned to the inst_Avail member variable of the object, so the value returned by instance.isInstAvail() in the main method will be null.

An easy way to fix this is swapping lines 3 and 4, so have_instance already has a value when the object is created. Or you could declare have_instance as boolean instead of Boolean, so it will have the value false and not null. This would make the program print nothing though.

Or maybe you could rethink what you're trying to do. It's rarely a good idea to create instances of a class before the class has finished initializing, especially if the class is not "final" (i.e. may have subclasses).

like image 173
Joni Avatar answered Mar 08 '26 02:03

Joni


The order of fields matter if you initialize these fields either directly by setting their values or using a static initializer block. They are executed in order. So you couldn't do a forward reference:

private int someInt = 10 + otherInt;
private int otherInt = 22;

This won't work, because the fields are initialized in order of their textual declaration. If you have two static initializers, they will be executed in order as well:

static { System.out.println("first"); }
static { System.out.println("second"); }

So in your case, you initialize instance before have_instance, so the latter is still null (default value for non-primitives). The JVM will create a MyClass object to be assigned to instance and initialize its fields, i.e. assign the value of have_instance to inst_Avail which will be set to null as well.

Some readings:

  • Object initialization in Java
like image 24
nif Avatar answered Mar 08 '26 00:03

nif



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!