简体   繁体   English

静态和非静态成员数据的初始化和构造函数的顺序

[英]static and non static member data initialization and constructor order

I am beginner in Java and stuck with simple concept of initialization of data and constructor calls when there is static member available in definition of class. 我是Java的初学者,当类的定义中有静态成员可用时,我坚持简单的数据初始化和构造函数调用的概念。

Code: 码:

class Bowl {
    Bowl(int marker) {
        System.out.println("Bowl(" + marker + ")");
    }
    void f1(int marker) {
        System.out.println("f1(" + marker + ")");
    }
}
class Table {
    static Bowl bowl1 = new Bowl(1);
    Table() {
        System.out.println("Table()");
        bowl2.f1(1);
    }
    void f2(int marker) {
        System.out.println("f2(" + marker + ")");
    }
    static Bowl bowl2 = new Bowl(2);
}
class Cupboard {
    Bowl bowl3 = new Bowl(3);
    static Bowl bowl4 = new Bowl(4);
    Cupboard() {
        System.out.println("Cupboard()");
        bowl4.f1(2);
    }
    void f3(int marker) {
        System.out.println("f3(" + marker + ")");
    }
    static Bowl bowl5 = new Bowl(5);
}
class StaticInitialization {
    public static void main(String[] args) {
        System.out.println("Creating new Cupboard() in main");
        new Cupboard();
        System.out.println("Creating new Cupboard() in main");
        new Cupboard();
        table.f2(1);
        cupboard.f3(1);
    }
    static Table table = new Table();
    static Cupboard cupboard = new Cupboard();
}

Output: 输出:

Bowl(1)
Bowl(2)
Table()
f1(1)
Bowl(4)
Bowl(5)
Bowl(3)
Cupboard()
f1(2)
Creating new Cupboard() in main
Bowl(3)
Cupboard()
f1(2)
Creating new Cupboard() in main
Bowl(3)
Cupboard()
f1(2)
f2(1)
f3(1)

I am unable to understand the flow of code and there is not much explanation in the book where this code appeared except that static members get initialized when they have not been initialized before and when it is necessary. 我无法理解代码的流程,本书中没有出现任何解释,除了静态成员在之前和必要时尚未初始化时会对其进行初始化。 Any leads? 有线索吗?

When your program starts, 当程序启动时,

1) StaticInitialization class is loaded into memory and its static fields are initialized first in the order they are described 1)将StaticInitialization类加载到内存中,并按照其描述的顺序首先初始化其静态字段

2) Static field table is initialized, that's why Table class is loaded into memory and all of its static fields are initialized. 2)初始化静态字段 ,这就是将Table类加载到内存中并初始化其所有静态字段的原因。

3) Static field bowl1 is initialized, that's why Bowl class is loaded into memory but it has no static fields, so the constructor is called and prints Bowl(1) 3)初始化静态字段bowl1 ,这就是为什么Bowl类被加载到内存中但没有静态字段的原因,因此调用了构造函数并打印Bowl(1)

4) Static field bowl2 is initialized, but Bowl class is already loaded, so its constructor is simply called and prints Bowl(2) 4)静态字段bowl2已初始化,但是Bowl类已经加载,因此简单地调用其构造函数并打印Bowl(2)

5) Table constructor is called, prints Table() and calls method f1(1) 5)调用构造函数,打印出Table()并调用方法f1(1)

6) Static field cupboard in StaticInitialization class is initalized, that's why Cupboard class is loaded into memory and all of its static fields are initialized. 6) 初始化了StaticInitialization类中的静态字段 ,这就是为什么将Cupboard类加载到内存中并初始化其所有静态字段的原因。

7) Static field bowl4 is initialized, but Bowl class is already loaded, so Bowl constructor is called and prints Bowl(4) 7)初始化静态字段bowl4 ,但是已经加载了Bowl类,因此调用Bowl构造函数并打印Bowl(4)

8) Same with bowl5 static field. 8)与bowl5静态字段相同。

9) Then, after all static fields of Cupboard are initialized, class members are initialized, that is bowl3 field. 9)然后,初始化Cupboard的所有静态字段后,将初始化类成员,即bowl3字段。 So, Bowl constructor is called and prints Bowl(3) 因此,调用Bowl构造函数并打印Bowl(3)

10) Cupboard constructor is called, prints Cupboard() and calls f1(2) method 10)调用Cupboard构造函数,打印Cupboard()并调用f1(2)方法

11) Main method starts to work. 11)主要方法开始起作用。 Prints Creating... and creates new Cupboard object, so class members of Cupboard are initialized. 打印正在创建...并创建新的Cupboard对象,因此初始化了Cupboard的类成员。 That is, bowl3 field is initialized within this new object and its constructor is called, so Bowl(3) is printed. 也就是说,在这个新对象中初始化了bowl3字段,并调用了它的构造函数,因此打印出Bowl(3)

12) Constructor of this new Cupboard object is called, it prints Cupboard() and calls f1(2) method of bowl3 object 12)调用此新Cupboard对象的构造方法,它打印Cupboard()并调用bowl3对象的f1(2)方法

13) Steps 11 and 12 are repeated. 13)重复步骤11和12。 (Main prints Creating... and creates new Cupboard object) (主要打印正在创建...并创建新的橱柜对象)

14) f2(1) and f3(1) methods are called one by one on different objects 14)在不同的对象上一一调用f2(1)f3(1)方法

Just remember that static fields are initialized once when the class loads into memory and class members are initialized by default when the object is created. 只需记住,当类加载到内存中时,静态字段将被初始化一次,并且在创建对象时默认情况下将初始化类成员。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM