Java中类的加载顺序剖析(常用于面试题)
作者:junjie 时间:2022-07-26 16:10:48
这其实是去年校招时我遇到的一道阿里巴巴的笔试题(承认有点久远了-。-),嗯,如果我没记错的话,当时是作为Java方向的一道选做大题。当然题意没有这么直白,题目只要求你写出程序运行后所有System.out.println的输出结果,其中程序是题目给的,而各个System.out.println的执行顺序不同会导致最后程序输出的结果也不同。
具体的题目我肯定记不清,不过我们可以换个直接的问法,如果类A和类B中有静态变量,静态语句块,非静态变量,非静态语句块,构造函数,静态方法,非静态方法,同时类A继承类B,请问当实例化A时,类内部的加载顺序是什么?
当时我也是一头雾水,事后我就自己写了一个小Demo,这才知道了类内部的实际加载顺,测试代码如下:
Class B:
public class B{
//静态变量
static int i=1;
//静态语句块
static {
System.out.println("Class B1:static blocks"+i);
}
//非静态变量
int j=1;
//静态语句块
static{
i++;
System.out.println("Class B2:static blocks"+i);
}
//构造函数
public B(){
i++;
j++;
System.out.println("constructor B: "+"i="+i+",j="+j);
}
//非静态语句块
{
i++;
j++;
System.out.println("Class B:common blocks"+"i="+i+",j="+j);
}
//非静态方法
public void bDisplay(){
i++;
System.out.println("Class B:static void bDisplay():"+"i="+i+",j="+j);
return ;
}
//静态方法
public static void bTest(){
i++;
System.out.println("Class B:static void bTest():"+"i="+i);
return ;
}
}
Class A:
public class A extends B{
//静态变量
static int i=1;
//静态语句块
static {
System.out.println("Class A1:static blocks"+i);
}
//非静态变量
int j=1;
//静态语句块
static{
i++;
System.out.println("Class A2:static blocks"+i);
}
//构造函数
public A(){
super();
i++;
j++;
System.out.println("constructor A: "+"i="+i+",j="+j);
}
//非静态语句块
{
i++;
j++;
System.out.println("Class A:common blocks"+"i="+i+",j="+j);
}
//非静态方法
public void aDisplay(){
i++;
System.out.println("Class A:static void aDisplay():"+"i="+i+",j="+j);
return ;
}
//静态方法
public static void aTest(){
i++;
System.out.println("Class A:static void aTest():"+"i="+i);
return ;
}
}
Class ClassLoading :
public class ClassLoading {
public static void main (String args[]) {
A a=new A();
a.aDisplay();
}
}
程序运行结果如图:
通过上述示图,我们可以比较清晰的看出java类的整个加载过程。
1.若要加载类A,则先加载执行其父类B(Object)的静态变量以及静态语句块(执行先后顺序按排列的先后顺序)。
2.然后再加载执行类A的静态变量以及静态语句块。(并且1、2步骤只会执行1次)
3.若需实例化类A,则先调用其父类B的构造函数,并且在调用其父类B的构造函数前,依次先调用父类B中的非静态变量及非静态语句块.最后再调用父类B中的构造函数初始化。
4.然后再依次调用类A中的非静态变量及非静态语句块.最后调用A中的构造函数初始化。( 并且3、4步骤可以重复执行)
5.而对于静态方法和非静态方法都是被动调用,即系统不会自动调用执行,所以用户没有调用时都不执行,主要区别在于静态方法可以直接用类名直接调用(实例化对象也可以),而非静态方法只能先实例化对象后才能调用。
OK,今天就总结到这里了,如果有地方说的不好或有错误的地方,欢迎大家指出,定当改正,谢谢。
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Java中&和&&的区别简单介绍
WinForm中Application.Idle方法详解
![](https://img.aspxhome.com/file/2023/5/69815_0s.jpg)
java并发编程专题(五)----详解(JUC)ReentrantLock
C#中ref和out的区别浅析
![](https://img.aspxhome.com/file/2023/0/106390_0s.png)
android获取ibeacon列表的方法
C#归并排序的实现方法(递归,非递归,自然归并)
![](https://img.aspxhome.com/file/2023/3/119503_0s.png)
Android实现底部滚轮式选择弹跳框
![](https://img.aspxhome.com/file/2023/7/111727_0s.jpg)
C#简单数字图像处理程序
![](https://img.aspxhome.com/file/2023/0/89780_0s.jpg)
Spring boot项目使用thymeleaf模板过程详解
C#采用OpenXml实现给word文档添加文字
![](https://img.aspxhome.com/file/2023/0/100240_0s.jpg)
Java发送post方法详解
C#使用GET、POST请求获取结果
C#中32位浮点数Float(Real)一步步按位Bit进行分析
![](https://img.aspxhome.com/file/2023/6/67046_0s.png)
SpringBoot绿叶显示yml和端口问题及解决方法
![](https://img.aspxhome.com/file/2023/5/62775_0s.png)
JDK 7U15在 Windows x86平台下的安装方法
![](https://img.aspxhome.com/file/2023/4/72324_0s.png)
Java4Android开发教程(二)hello world!
![](https://img.aspxhome.com/file/2023/7/123237_0s.png)
Java 切割字符串的几种方式集合
![](https://img.aspxhome.com/file/2023/9/84329_0s.jpg)
C#使用Data Annotations进行手动数据验证
![](https://img.aspxhome.com/file/2023/5/122755_0s.gif)
Android中TextureView与SurfaceView用法区别总结
![](https://img.aspxhome.com/file/2023/2/101852_0s.jpg)
SpringBoot3.0自定stater模块的操作流程(chatGPT提供的49种场景)
![](https://img.aspxhome.com/file/2023/3/68023_0s.png)