Creating object using static keyword in Java

后端 未结 2 1082
萌比男神i
萌比男神i 2021-02-19 08:41
class abc {
    int a = 0;
    static int b;
    static abc h = new abc(); //line 4

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

    {
        System         


        
相关标签:
2条回答
  • 2021-02-19 08:52

    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.

    0 讨论(0)
  • 2021-02-19 09:07

    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.

    0 讨论(0)
提交回复
热议问题