java----类的加载与其初始化
创始人
2024-04-23 15:33:58
0

java内存分析:

在这里插入图片描述

类加载的过程:

在这里插入图片描述

类的加载与ClassLoader的理解:

在这里插入图片描述

类的初始化:

package Collections;
public class text1 {
public static void main(String[]args){A a=new A();System.out.println(A.m);
}
}
class A{static {System.out.println("A类静态代码块初始化");m=300;}static int m=100;public A(){System.out.println("A类的无参构造初始化");}
}

输出:

A类静态代码块初始化
A类的无参构造初始化
100

为什么最终输出的m值为100呢?

分析如下:

首先类进行加载和链接,如下所示:

在这里插入图片描述
注:在链接阶段的准备工作时,编译器会为类变量赋默认值为0,即此时的m为0

链接完毕后进行类的初始化,这一过程将会执行类构造器()方法,将类中所有类变量的赋值语句以及静态代码块中的语句收集和合并:

 () {System.out.println("A类静态代码块初始化");m=300;m=100;}

第二条m的赋值语句,覆盖了第一条的300,因此最终输出为100

会发生类的初始化的场景:

类的主动引用(一定会发生类的初始化)

在这里插入图片描述

类的主动引用:
package Collections;import static Collections.Son.m;public class text1 {static {System.out.println("Main类被加载");}
public static void main(String[]args) throws ClassNotFoundException {Son son=new Son();
}
}
class Father{static int a=10;static{System.out.println("Father类被加载");}
}
class Son extends Father{
static {System.out.println("子类被加载");m= 300;
}static int m =100;static final int M = 1;
}

输出:

Main类被加载
Father类被加载
子类被加载
反射也会产生主动引用:
package Collections;import static Collections.Son.m;public class text1 {static {System.out.println("Main类被加载");}
public static void main(String[]args) throws ClassNotFoundException {Class.forName("Collections.Son");
}
}
class Father{static int a=10;static{System.out.println("Father类被加载");}
}
class Son extends Father{
static {System.out.println("子类被加载");m= 300;
}static int m =100;static final int M = 1;
}

输出:

Main类被加载
Father类被加载
子类被加载

类的被动引用(不会发生类的初始化):

当访问一个静态域时,只有真正声明这个域的类才会被初始化,如:当通过子类引用父类的静态变量,不会导致子类初始化。

举例:

package Collections;import static Collections.Son.m;public class text1 {static {System.out.println("Main类被加载");}
public static void main(String[]args) throws ClassNotFoundException {System.out.println(Son.a);
}
}
class Father{static int a=10;static{System.out.println("Father类被加载");}
}
class Son extends Father{
static {System.out.println("子类被加载");m= 300;
}static int m =100;static final int M = 1;
}

输出:

Main类被加载
Father类被加载
10

通过数组定义类引用,不会触发此类的初始化:

举例:

package Collections;import static Collections.Son.m;public class text1 {static {System.out.println("Main类被加载");}
public static void main(String[]args) throws ClassNotFoundException {Son[] arry=new Son[5];
}
}
class Father{static int a=10;static{System.out.println("Father类被加载");}
}
class Son extends Father{
static {System.out.println("子类被加载");m= 300;
}static int m =100;static final int M = 1;
}

输出:

Main类被加载

引用常量不会触发此类的初始化(常量在链接阶段就存入调用类的常量池中了)

举例:

package Collections;import static Collections.Son.m;public class text1 {static {System.out.println("Main类被加载");}
public static void main(String[]args) throws ClassNotFoundException {System.out.println(Son.M);
}
}
class Father{static int a=10;static{System.out.println("Father类被加载");}
}
class Son extends Father{
static {System.out.println("子类被加载");m= 300;
}static int m =100;static final int M = 1;
}

输出:

Main类被加载
1

相关内容

热门资讯

监控摄像头接入GB28181平... 流程简介将监控摄像头的视频在网站和APP中直播,要解决的几个问题是:1&...
Windows10添加群晖磁盘... 在使用群晖NAS时,我们需要通过本地映射的方式把NAS映射成本地的一块磁盘使用。 通过...
protocol buffer... 目录 目录 什么是protocol buffer 1.protobuf 1.1安装  1.2使用...
在Word、WPS中插入AxM... 引言 我最近需要写一些文章,在排版时发现AxMath插入的公式竟然会导致行间距异常&#...
【PdgCntEditor】解... 一、问题背景 大部分的图书对应的PDF,目录中的页码并非PDF中直接索引的页码...
Fluent中创建监测点 1 概述某些仿真问题,需要创建监测点,用于获取空间定点的数据࿰...
educoder数据结构与算法...                                                   ...
MySQL下载和安装(Wind... 前言:刚换了一台电脑,里面所有东西都需要重新配置,习惯了所...
修复 爱普生 EPSON L4... L4151 L4153 L4156 L4158 L4163 L4165 L4166 L4168 L4...
MFC文件操作  MFC提供了一个文件操作的基类CFile,这个类提供了一个没有缓存的二进制格式的磁盘...