14
class abc {
    int a = 0;
    static int b;
    static abc h = new abc(); //line 4

    public abc() {
        System.out.println("cons");
    }

    {
        System.out.println("ini");
    }

    static {
        System.out.println("stat");
    }
}

public class ques {
    public static void main(String[] args) {
        System.out.println(new abc().a);
    }
}

When i wrote this code I am getting output in order like this:

ini
cons
stat
ini
cons
0

Here when I created a new object in main(), class abc got loaded and static variables and blocks are executed in order they are written. When control came to line 4 static abc h = new abc(); Instance Initialization block is called. Why? why is static block not called when a new object is created at line 4 and till that time static block was also not called even once, so according to convention static block should have been called. Why is this unexpected output coming?

10
  • 1
    possible duplicate of Java static class initialization Commented Jul 24, 2014 at 7:28
  • Static code is run only once so when the second object is created (inside the first one) it is not executed. I am surprised that ini is executed at this point though. Commented Jul 24, 2014 at 7:37
  • 2
    @DavidPostill- Tagged post clears the concept that how static initialization normally happens. But my main ques is that why static block is not called at line 4 and instead Instance Initialization block was called. Commented Jul 24, 2014 at 7:37
  • 1
    As I debug your code, I can see that at line 4 static block is called and that is only place it will be called due to static initialization. If you remove the static member static block is called first. Commented Jul 24, 2014 at 7:41
  • @Duncan I just wanted to make the instance initializer easier to see, but I will not argue if you say so. In retrospect, you are actually right, I should have added 1 newline not 2. Commented Jul 24, 2014 at 7:45

2 Answers 2

7

JLS says:

The static initializers and class variable initializers are executed in textual order, and may not refer to class variables declared in the class whose declarations appear textually after the use, even though these class variables are in scope (§8.3.2.3). This restriction is designed to detect, at compile time, most circular or otherwise malformed initializations.

Which is exactly your case.

Here is your original example: http://ideone.com/pIevbX - static initializer of abc goes after static instance of abc is assigned - so static initializer can't be executed - it's textually after static variable initialization

Let's move line 4 after static initialization block - http://ideone.com/Em7nC1 :

class abc{
int a = 0;
static int b;
public abc() {
    System.out.println("cons");
}
{
    System.out.println("ini");
}
static {
    System.out.println("stat");
}
static abc h = new abc();//former line 4

}

Now you can see the following output:

stat
ini
cons
ini
cons
0

Now initialization order is more like you expected - first called static initializer and then static instance of abc is initialized in common manner.

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

Comments

6

Static fields initialization and static blocks are executed in the order they are declared. In your case, the code is equivalent to this after separating declaration and initialization:

class abc{
    int a;
    static int b;
    static abc h;//line 4

    static {
        h = new abc();//line 4 (split)
        System.out.println("stat");
    }

    public abc() {
        a = 0;
        System.out.println("ini");
        System.out.println("cons");
    }
}

public class ques{
    public static void main(String[] args) {
        System.out.println(new abc().a);
    }
}

So when you reach line 4 from your code, the static initialization is actually being executed and not finished yet. Hence your constructor is called before stat can be printed.

Comments

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.