Java中类初始化代码块和父类构造器执行顺序问题,
阿神
阿神 2017-04-18 10:10:52
0
2
386
阿神
阿神

闭关修行中......

reply all(2)
洪涛

I searched for a long time but couldn’t find the execution time of the block. Later, I used javap -c to check the class file and found that the block was thrown into the constructor, and the <init> method of invokespecial superclass was also in it, but it was in front of the block, so this Explained your problem.

小葫芦

First of all, after using the javac command to compile a java class, the initialization code block of the java class will disappear - the code in the initialization code block will be "restored" to each constructor and will be located in front of all the code in the constructor.

Basic process:

1.为要创建的对象分配内存空间,其大小取决于Java类及其父类和祖先类包含的所有实例域的数量和类型
2.如果内存分配成功(失败会抛出OutOfMemoryError错误),把新创建的对象的所有实例域(包括父类的)都设为默认值
3.调用构造方法
    ①调用父类的构造方法,如果没有显式使用super关键字进行调用,则由编译器自动生成相关的代码
    ②按照类中实例域的顺序初始化实例域的值
    ③执行类的构造方法中的其他代码完成最终的初始化工作

You can analyze it yourself.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!