java中带继承类的加载顺序详解及实战
一、背景:
在面试中,在java基础方面,类的加载顺序经常被问及,很多时候我们是搞不清楚到底类的加载顺序是怎么样的,那么今天我们就来看看带有继承的类的加载顺序到底是怎么一回事?在此记下也方便以后复习巩固!
二、测试步骤:
1.父类代码
package com.hafiz.zhang; public class Fu
{
private int i = print("this is father common variable");
private static int j = print("this is father static variable");
static{
System.out.println("this is father static code block");
}
{
System.out.println("this is father common code block");
}
public Fu(){
System.out.println("this is father constructor");
} static int print(String str){
System.out.println(str);
return 2;
}
}
2.子类代码
package com.hafiz.zhang; public class Zi extends Fu
{
private int l = print("this is son common variable");
private static int m = print("this is son stati variable");
static{
System.out.println("this is son static code block");
}
{
System.out.println("this is son common code block");
}
public Zi(){
System.out.println("this is son constructor");
}
public static void main(String[] args) {
new Zi();
}
}
最后运行结果为:
下面让我们修改一下两个类中静态代码块和静态成员变量的位置并重新运行
3.修改后的父类代码
package com.hafiz.zhang; public class Fu
{
static{
System.out.println("this is father static code block");
}
{
System.out.println("this is father common code block");
}
public Fu(){
System.out.println("this is father constructor");
} static int print(String str){
System.out.println(str);
return 2;
}
private int i = print("this is father common variable");
private static int j = print("this is father static variable");
}
4.修改后的子类代码
package com.hafiz.zhang; public class Zi extends Fu
{
static{
System.out.println("this is son static code block");
}
{
System.out.println("this is son common code block");
}
public Zi(){
System.out.println("this is son constructor");
}
public static void main(String[] args) {
new Zi();
}
private int l = print("this is son common variable");
private static int m = print("this is son stati variable");
}
修改后的运行结果:
三、测试结果
由测试结果可知:程序首先加载类,然后再对类进行初始化。
加载类的顺序为:先加载基类,基类加载完毕后再加载子类。
初始化的顺序为:先初始化基类,基类初始化完毕后再初始化子类。
最后得出类加载顺序为:先按照声明顺序初始化基类静态变量和静态代码块,接着按照声明顺序初始化子类静态变量和静态代码块,而后按照声明顺序初始化基类普通变量和普通代码块,然后执行基类构造函数,接着按照声明顺序初始化子类普通变量和普通代码块,最后执行子类构造函数。
对于本测试中的执行顺序为:先初始化static的变量,在执行main()方法之前就需要进行加载。再执行main方法,如果new一个对象,则先对这个对象类的基本成员变量进行初始化(非方法),包括构造代码块,这两种是按照编写顺序按序执行的,再调用构造函数。 关于继承的初始化机制,首先执行含有main方法的类,观察到Zi类含有基类Fu,即先加载Fu类的static变量,再加载Zi类的static变量。加载完static变量之后,调用main()方法,new Zi()则先初始化基类的基本变量和构造代码块,再调用基类的构造方法。然后再初始化子类Zi的基本变量和构造代码块,再执行子类的构造函数。
java中带继承类的加载顺序详解及实战的更多相关文章
- (转)面试题--JAVA中静态块、静态变量加载顺序详解
public class Test { //1.第一步,准备加载类 public static void main(String[] args) { new Test(); //4.第四步,new一个 ...
- JAVA中静态块、静态变量加载顺序详解
http://blog.csdn.net/mrzhoug/article/details/51581994 一般顺序:静态块(静态变量)——>成员变量——>构造方法——>静态方法
- Spring Boot 配置加载顺序详解
使用 Spring Boot 会涉及到各种各样的配置,如开发.测试.线上就至少 3 套配置信息了.Spring Boot 可以轻松的帮助我们使用相同的代码就能使开发.测试.线上环境使用不同的配置. 在 ...
- servlet与filter的加载顺序详解
项目:3个filter,3个servlet,匹配的url路径/hello. 情况1:servlet没加<load-on-startup></load-on-startup>情 ...
- dubbo配置文件的加载顺序详解(图示)
Dubbo配置文件的加载顺序 在使用apache dubbo.version2.7.3 时,配置文件的加载情况.以provider提供服务者为例. 配置文件 ,以下四个配置文件. 其优先级 app ...
- 面试题--JAVA中静态块、静态变量加载顺序
最后给大家一道面试题练练手,要求写出其结果(笔试) public class StaticTest { public static int k = 0; public static StaticTes ...
- Javascript在浏览器中的加载顺序详解!
现在前端用javascript用的比较多,当然真心的说这个语言是一个非常业余的语言,但是用的人很多,所以也比较火.今天想完成一个javascript外部文件自动加载的设计(类似于java或者php的i ...
- web.xml加载顺序详解
一. 1.启动tomcat启动web项目,首先读取web.xml文件中<context-param>和<listener> 2.容器创建一个ServletContext(ser ...
- Java中使用Scanner类进行键盘的输入详解
我们在使用Java写代码时,经常会遇到从键盘输入字符串等操作,这时候我们需要用到的是我们的Scanner类来实现获取用户从键盘上的输入操作. Scanner类是一个基于正则表达式的文本扫描器,它可以从 ...
随机推荐
- 【学习】一本案例驱动的jQuery Mobile入门书
清华大学出版社推出的<构建跨平台APP:jQuery Mobile移动应用实战> 提供的全是jQuery Mobile的案例: 手机入侵 题库应用 音乐专辑 通讯录 新闻列表 销售排名 九 ...
- phpcms标签云
{pc:get sql="SELECT keyword FROM v9_keyword WHERE siteid=$siteid AND searchnums > 5 ORDER BY ...
- CDN技术发展趋势
智能化 存储智能化 关键技术点:文件分片热度的判断策略,跨节点快速文件调度 存储向以文件切片为基础的全网共享存储发展:把一个整片切成n个小块,按用户访问热度分散存储在不同节点.当用户访问的时候,由边缘 ...
- nhibernat4.0.0.4000 bug
//类 NHibernate.Loader.Loader 中 protected virtual string[] ResultRowAliases { get { return null; } } ...
- BZOJ 1019: [SHOI2008]汉诺塔
Description 一个汉诺塔,给出了移动的优先顺序,问从A移到按照规则移到另一个柱子上的最少步数. 规则:小的在大的上面,每次不能移动上一次移动的,选择可行的优先级最高的. Sol DP. 倒着 ...
- 关于ubuntukylin安装后界面中英文混杂的问题
起因 一直使用的是ubuntu原版的系统,ubuntukylin出来后也没用使用过.一次去其论坛逛了一圈之后决定使用一下. 安装后的截面和ubuntu原版的差不多,还是挺漂亮的. 但是有一个问题是,安 ...
- silk与opencore-amr音频编码对比
silk与opencore-amr编码对比 在采样率8000 单声道 16位采样精度情况下 silk的压缩率为 1/15 opencore-amr 1/17 对比图 原始的音频编码 opencore- ...
- OpenMP求完数
源代码: #include "stdafx.h" //必须写在首行,因为其前面的include都会被忽略 #include "omp.h" #include & ...
- SQL Server数据库主键与索引的几点区别
我们在使用SQL Server数据库的时候常常会创建主键和索引,那么主键和索引到底有什么样的不同呢?本文我们主要介绍了主键和索引的区别. 主键和索引的区别如下: 主键是索引,但索引不一定是主键. 主键 ...
- json和对象、list互转
1.把对象,list转成json: import com.alibaba.fastjson.JSONObject; Object obj = new Object(); // ... String o ...