Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Java创建对象的过程(以Dog类为例) #3

Open
chengwen-zheng opened this issue Jan 16, 2018 · 0 comments
Open

Java创建对象的过程(以Dog类为例) #3

chengwen-zheng opened this issue Jan 16, 2018 · 0 comments
Labels

Comments

@chengwen-zheng
Copy link
Owner

chengwen-zheng commented Jan 16, 2018

  1. 即使没有显式的使用static关键字,构造器实际上也是静态方法。因此当首次创建类型为Dog的对象或者Dog类的静态方法/静态域首次被访问时,Java解释器必须查找类路径,以定位Dog.class文件。
  2. 然后载入Dog.class,创建一个Class对象,有关静态初始化的所有动作都会执行。因此,静态初始化只在class对象首次加载的时候进行一次。
  3. 当用new Dog() 创建对象时候,首先会在堆上为Dog对象分配足够的存储空间。
  4. 这块储存空间会被清零。这就自动地将Dog对象中所有的基本类型数据都设置成了默认值(对数字来说为0,对布尔类型和字符型也相同),而引用被设置成了null.
  5. 执行所有出现于字段定义处的初始化动作。
  6. 执行构造器。注意其中会子类的构造器也会调用。也就是在父类中有个对子类对象的引用。可看成是父类的域对象,用super.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant