2

I would like to ask why the following code causes error:

class A
{
    A()
    {
       statObj.x = 5; 
    }

    int x;
    static A statObj = new A();
}

I get ExceptionInInitializerError. I don't understand why. In this case static variable statObj will be initialized as the first one. So, if I am right, static object statObj = new A() will be create as the first one.

What is the order of creation and initialization of this internal static object? Isn't statObj.x default initialized by 0 value, before the internal static object constructor is called statObj.A() ? If that ,why statObj.x behaves like it wasn't initialized (I fought it was default initialized by 0 value) ?

And one more why this problem only occurs in constructor and not in method ? The following code works fine:

        class A
        {
            A()
            {  
            }

            void met1()
            {
                statObj.x = 5; 
            }

            int x;
            static A statObj = new A();
        }


        public MainClass
        {
            public static void main(String[] arg)
            {
                A a = new A();
                a.statObj.met1();
            }
        }
1
  • 3
    Why do you think it would work? Commented Dec 12, 2012 at 11:37

4 Answers 4

9

Consider what happens. The following static initializer:

static A statObj = new A();

calls A(), which then tries to access statObj:

A() {
  statObj.x = 5; 
}

However, at this point statObj is not yet initialized and is therefore null. A NullPointerException is thrown, which then gets translated into an ExceptionInInitializerError (since the NPE has occurred in a static initializer).

The second example does not have this problem, since by the time you try to access statObj, it has been fully initialized.

Sign up to request clarification or add additional context in comments.

2 Comments

Ok I understand your explanation, but still don't uderstand the code behaviour. If create new object the order of creation is like this: - find A.class file, - initialization of static variables, - default initialzation of non static variables, - allocation memory for the object - explicit initialization of non static variables, - constructor execution So when we create statObj, shouldn't statObj.x be default initialize (by 0) before the statObj constructor is called ? If not, than why we can do something like this: class B { int y; B() { y = 5; } ?
@IzaMarek: shouldn't statObj.x be default initialize (by 0) before the statObj constructor is called. Well, no. Before the constructor is called and finishes there is no statObj (i.e. the reference is null).
0

You are trying to access an object (statObj.x) before it has been created. Try this instead:

class A
{
    A()
    { 
    }

    int x;
    static A statObj = new A();

    static {
        statObj.x = 5;
    }
}

Comments

0

The problem is that you intialized a static with constructor that contain this static.

To finish properly construcor should have that static value. But to have that static value construcor must be finished first.

Comments

0

You are getting a NullPointerException, because you are trying to access the instance stored in the static field before that object has been created. This is happening because it only gets created after its constructor completes. But you are calling this from within the constructor.

The second code works fine, because there the constructor (which does nothing) has a chance to complete.

Again:

 static A statObj = new A();

statObj will be null until the constructor (new A()) completes.

If create new object the order of creation is like this: - find A.class file, - initialization of static variables, - default initialzation of non static variables, - allocation memory for the object - explicit initialization of non static variables, - constructor execution

Well, the problem here is that during "initialization of static variables" you are calling the constructor that tries to use the same static variable, jumping a bit ahead in your sequence if you will.

So when we create statObj, shouldn't statObj.x be default initialize (by 0) before the statObj constructor is called ?

Yes, but that is not the problem here. The problem here is that statObj itself is null before its constructor finishes.

statObj.x = 5;

If you did this.x, it would work. But statObj is still null at the time.

1 Comment

Please check comment that I wrote to @NPE answer

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.