JVM 类的生命周期、类加载器
类的加载、连接与初始化
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
|
public class Test { public static void main(String[] args) { Count count = Count.getInstance(); System.out.println( "count1 = " + count.count1); System.out.println( "count2 = " + count.count2); } } class Count { // 这个运行结果是 count1 = 1 count2 = 0 ; 因为按顺序执行1. Count(); 2. count1; 3. count2; private static Count count = new Count(); public static int count1; public static int count2 = 0 ; // 所以这个运行结果是 count1 = 1 count2 = 1 ; // private static Count count = new Count(); private Count() { count1++; count2++; } public static Count getInstance() { return count; } } |
1. 类的加载
2. 连接
2.1 类的验证
2.2 类的准备
1
2
3
4
5
6
7
8
9
|
public class Sample { private static int a = 1 ; private static long b; static { b = 2 ; } // ... } |
2.3 类的解析
1
2
3
|
public void gotoWord(){ car.run(); //这段代码在Worker类的二进制数据中表示为符号引用 } |
3. 类的初始化
1
2
3
4
5
6
7
8
9
10
|
public class Sample { private static int a = 1 ; private static long b; private static long c; static { b = 2 ; } // ... } |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
class Sample { private static int a = 1 ; static { a = 2 ; } static { a = 4 ; } public static void main(String[] args) { System.out.println(a); // 输出4 } } |
3. 1 类的初始化时机
以下代码可以加深理解:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
|
public class Test { public static void main(String[] args) { // x是一个编译时的常量,编译的时候就知道值是多少,不需要对类进行初始化 System.out.println(FinalTest.x); // x非编译时的常量,x在编译时不知道是多少, // 运行才知道的就需要对类进行初始化,对类进行初始化static代码快就会执行 System.out.println(FinalTest2.x); } } class FinalTest { public static final int x = 6 / 3 ; static { System.out.println( "FinalTest staic block!" ); } } class FinalTest2 { public static final int x = new Random().nextInt( 100 ); static { System.out.println( "FinalTest2 staic block!" ); } } |
运行结果:
当 JVM 初始化一个类时,要求它的所有父类都己经被初始化,但是这条规则并不适用于接口。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
|
public class Test { static { System.out.println( "Test static block!" ); } public static void main(String[] args) { System.out.println(Child.b); } } class Parent { static int a = 3 ; static { System.out.println( "Parent static block!" ); } } class Child extends Parent { static int b = 4 ; static { System.out.println( "Child static block!" ); } } |
运行结果:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
|
public class Test { public static void main(String[] args) { System.out.println(Child.a); Child.doSomething(); } } class Parent { static int a = 3 ; static { System.out.println( "Parent static block!" ); } static void doSomething() { System.out.println( "do something!" ); } } class Child extends Parent { static { System.out.println( "Child static block!" ); } } |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
public class Test { public static void main(String[] args) throws ClassNotFoundException { // 获取系统类加载器 ClassLoader loader = ClassLoader.getSystemClassLoader(); // 这行代码没有导致任何输出 不会导致类的初始化 Class<?> clazz = loader.loadClass( "CL" ); System.out.println( "------" ); clazz = Class.forName( "CL" ); } } class CL { static { System.out.println( "Class CL" ); } } |
类加载器
扩展(Extension)类加载器
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
public class Test { public static void main(String[] args) throws ClassNotFoundException { // String 是由根类加载器加载的,下面打印结果为null Class<?> clazz = Class.forName( "java.lang.String" ); System.out.println(clazz.getClassLoader()); // 应用加载器加载的 Class<?> clazz2 = Class.forName( "C" ); System.out.println(clazz2.getClassLoader()); } } class C { } |
类加载的父委托机制
1
2
3
4
|
ClassLoader loader1 = new MyClassLoader(); // 参数loader1将作为loader2的父加载器 ClassLoader loader2 = new MyClassLoader(loader1); |
命名空间
每个类加载器都有自己的命名空间,命名空间由该加载器及所有父加载器所加载的类组成。在同一个命名空间中,不会出现类的完整名字(包括类的包名)相同的两个类;在不同的命名空间中,有可能会出现类的完整名字(包括类的包名)相同的两个类。
运行时包(package)
创建用户自定义的类加载器
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
|
public class MyClassLoader extends ClassLoader { // 类加载器名字 private String name; // 加载类的路径 private String path = "d:\\" ; // class文件的扩展名 private final String fileType = ".class" ; public MyClassLoader(String name) { super (); // 让系统类加载器成为该类加载器的父加载器 this .name = name; } public MyClassLoader(ClassLoader parent, String name) { super (parent); // 显示指定该类加载器的父加载器 this .name = name; } public String toString() { return this .name; } public String getPath() { return path; } public void setPath(String path) { this .path = path; } @Override protected Class<?> findClass(String name) throws ClassNotFoundException { byte [] data = this .loadClassData(name); return this .defineClass(name, data, 0 , data.length); } private byte [] loadClassData(String name) { InputStream is = null ; byte [] data = null ; ByteArrayOutputStream baos = null ; try { this .name = this .name.replace( "." , "\\" ); is = new FileInputStream( new File(path + name + fileType)); baos = new ByteArrayOutputStream(); int ch = 0 ; while (- 1 != (ch = is.read())) { baos.write(ch); } data = baos.toByteArray(); } catch (Exception e) { e.printStackTrace(); } finally { try { baos.close(); is.close(); } catch (IOException e) { e.printStackTrace(); } } return data; } public static void main(String[] args) throws Exception { // 父加载器为系统类加载器 MyClassLoader loader1 = new MyClassLoader( "loader1" ); loader1.setPath( "D:\\test\\serverlib\\" ); // 指定loader2的父加载器为loader1 MyClassLoader loader2 = new MyClassLoader(loader1, "loader2" ); loader2.setPath( "D:\\test\\clientlib\\" ); // 指定loader3的父加载器为根加载器 MyClassLoader loader3 = new MyClassLoader( null , "loader3" ); loader3.setPath( "D:\\test\\otherlib\\" ); test(loader2); test(loader3); } public static void test(ClassLoader loader) throws Exception { Class clazz = loader.loadClass( "Sample" ); Object object = clazz.newInstance(); } } |
1
2
3
4
5
6
|
public class Dog { public Dog() { System.out.println( "Dog is load by : " + this .getClass().getClassLoader()); } } |
1
2
3
4
5
6
7
8
9
10
11
12
|
public class Sample { public int v1 = 1 ; public Sample() { System.out.println( "Sample is load by : " + this .getClass().getClassLoader()); // 主动使用Dog new Dog(); } } |
1
2
3
4
5
|
D:\myapp\syslib>java MyClassLoader Sample is load by : loader1 Dog is load by : loader1 Sample is load by : loader3 Dog is load by : loader3 |
1
2
3
4
5
|
D:\myapp\syslib>java MyClassLoader Sample is load by : sun.misc.Launcher$AppClassLoader@659e0bfd Dog is load by : sun.misc.Launcher$AppClassLoader@659e0bfd Sample is load by : loader3 Dog is load by : loader3 |
1
2
3
4
5
|
D:\myapp\syslib>java - cp .;d:\myapp\serverlib MyClassLoader Sample is load by : sun.misc.Launcher$AppClassLoader@659e0bfd Dog is load by : sun.misc.Launcher$AppClassLoader@659e0bfd Sample is load by : loader3 Dog is load by : loader3 |
1
2
3
4
5
|
D:\myapp\syslib>java MyClassLoader Sample is load by : loader1 Dog is load by : sun.misc.Launcher$AppClassLoader@659e0bfd Sample is load by : loader3 Dog is load by : loader3 |
1
2
3
4
5
6
7
8
9
10
11
|
public static void main(String[] args) throws Exception { // 父加载器为系统类加载器 MyClassLoader loader1 = new MyClassLoader( "loader1" ); loader1.setPath( "D:\\myapp\\serverlib\\" ); Class clazz = loader1.loadClass( "Sample" ); Object object = clazz.newInstance(); // 创建对象 Sample sample = (Sample)object; System.out.println(sample.v1); } |
1
2
3
4
5
6
7
8
9
10
11
|
D:\myapp\syslib>java MyClassLoader Sample is load by : loader1 Dog is load by : loader1 Exception in thread "main" java.lang.NoClassDefFoundError: Sample at MyClassLoader.main(MyClassLoader.java:110) Caused by: java.lang.ClassNotFoundException: Sample at java.net.URLClassLoader.findClass(Unknown Source) at java.lang.ClassLoader.loadClass(Unknown Source) at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source) at java.lang.ClassLoader.loadClass(Unknown Source) ... 1 more |
1
2
3
4
5
6
7
8
9
10
11
12
|
public static void main(String[] args) throws Exception { // 父加载器为系统类加载器 MyClassLoader loader1 = new MyClassLoader( "loader1" ); loader1.setPath( "D:\\myapp\\serverlib\\" ); Class clazz = loader1.loadClass( "Sample" ); Object object = clazz.newInstance(); // 创建对象 Field field = clazz.getField( "v1" ); int v1 = field.getInt(object); System.out.println( "v1:" + v1); } |
1
2
3
4
|
D:\myapp\syslib>java MyClassLoader Sample is load by : loader1 Dog is load by : loader1 v1:1 |
类的卸载
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
public static void main(String[] args) throws Exception { // 父加载器为系统类加载器 MyClassLoader loader1 = new MyClassLoader( "loader1" ); //1 loader1.setPath( "D:\\myapp\\serverlib\\" ); //2 Class objClass = loader1.loadClass( "Sample" ); //3 System.out.println( "objClass's hashCode is " + objClass.hashCode()); //4 Object obj = objClass.newInstance(); // 创建对象 //5 loader1 = null ; //6 objClass = null ; //7 obj = null ; //8 loader1 = new MyClassLoader( "loader1" ); //9 objClass = loader1.loadClass( "Sample" ); //10 System.out.println( "objClass's hashCode is " + objClass.hashCode()); //11 } |
1
2
3
4
5
|
D:\myapp\syslib>java MyClassLoader objClass's hashCode is 1311053135 Sample is load by : loader1 Dog is load by : loader1 objClass's hashCode is 865113938 |
JVM 类的生命周期、类加载器的更多相关文章
- Java - JVM - 类的生命周期
概述 简述 JVM 里 类的生命周期 上次写了 30%, 居然丢了 难受, 又要重新写 类的生命周期 加载 使用 卸载 1. 加载 概述 类型的加载 大体流程 装载 连接 验证 准备 解析(可选的) ...
- JVM类加载器及Java类的生命周期
预定义类加载器(三种): 启动(Bootstrap)类加载器: 是用本地代码实现的类装入器,它负责将<Java_Runtime_Home>/lib下面的类库加载到内存中(比如rt.jar) ...
- 乐字节Java反射之三:方法、数组、类加载器和类的生命周期
本文承接上一篇:乐字节Java发射之二:实例化对象.接口与父类.修饰符和属性 继续讲述Java反射之三:方法.数组.类加载器 一.方法 获取所有方法(包括父类或接口),使用Method即可. publ ...
- JVM:类的生命周期
类的生命周期 综述 1. 只有当一个类被切实使用到的时候才会被加载到虚拟机中(例如:new, 方法调用, A a = null;不算) 2. 若在加载一个类的过程中,有其他类被切实使用到, ...
- <JVM中篇:字节码与类的加载篇>03-类的加载过程(类的生命周期)详解
笔记来源:尚硅谷JVM全套教程,百万播放,全网巅峰(宋红康详解java虚拟机) 同步更新:https://gitee.com/vectorx/NOTE_JVM https://codechina.cs ...
- JVM与垃圾回收机制(GC)和类的生命周期
JVM运行时数据区 GC(垃圾回收机制) 什么是垃圾回收机制: 在系统运行过程中,会产生一些无用的对象,这些对象占据着一定的内存,如果不对这些对象清理回收无用的是对象,可能会导致内存的耗尽,所以垃圾回 ...
- Java类的生命周期详解
引言 最近有位细心的朋友在阅读笔者的文章时,对java类的生命周期问题有一些疑惑,笔者打开百度搜了一下相关的问题,看到网上的资料很少有把这个问题讲明白的,主要是因为目前国内java方面的教材大多只是告 ...
- 【转】Java 类的生命周期详解
一. 引 言 最近有位细心的朋友在阅读笔者的文章时,对java类的生命周期问题有一些疑惑,笔者打开百度搜了一下相关的问题,看到网上的资料很少有把这个问题讲明白的,主要是因为目前国内java方面的教材大 ...
- 【转载】详解java类的生命周期
原文地址:http://blog.csdn.net/zhengzhb/article/details/7517213 引言 最近有位细心的朋友在阅读笔者的文章时,对java类的生命周期问题有一些疑惑, ...
随机推荐
- Tomcat安装及问题排查方法
简介: Apache Jakarta的开源项目 JSP/Servlet容器 安装: 1.1进入 Tomcat 官方下载地址 选择合适版本下载,并解压到本地. (备注)Tomcat 8.5 要求 JDK ...
- Docker 容器格式
最初,Docker 采用了 LXC 中的容器格式.自 1.20 版本开始,Docker 也开始支持新的 libcontainer 格式,并作为默认选项. 对更多容器格式的支持,还在进一步的发展中.
- Python3 元组
Python 的元组与列表类似,不同之处在于元组的元素不能修改. 元组使用小括号,列表使用方括号. 元组创建很简单,只需要在括号中添加元素,并使用逗号隔开即可. 如下实例: tup1 = ('Goog ...
- Python 3.3.2 round函数并非"四舍五入"
对于一些貌似很简单常见的函数,最好还是去读一下Python文档,否则当你被某个BUG折磨得死去活来时,还不知根源所在.尤其是Python这种不断更新的语言.(python 2.7 的round和3.3 ...
- Latex:TexStudio的使用
http://blog.csdn.net/pipisorry/article/details/54565608 Texsdudio 快捷键 The keyboard shortcuts can be ...
- android Git命令家底儿及Git数据通信原理详解
声明:本文为CSDN原创投稿文章,未经许可,禁止任何形式的转载. 现在大部分使用的都是SVN,也有一部分迁移了Git,虽然挺好的,不过还有其它很多版本控制的工具,并没有谁最好用,最重要的是适合自己的公 ...
- NLP系列(3)_用朴素贝叶斯进行文本分类(下)
作者: 龙心尘 && 寒小阳 时间:2016年2月. 出处: http://blog.csdn.net/longxinchen_ml/article/details/50629110 ...
- PGM:贝叶斯网的参数估计2
http://blog.csdn.net/pipisorry/article/details/52599321 没时间看了,下次再看... 具有共享参数的学习模型 全局参数共享 局部参数共享 具有 共 ...
- ROS机器人程序设计(原书第2版)补充资料 (玖) 第九章 导航功能包集进阶 navigation
ROS机器人程序设计(原书第2版)补充资料 (玖) 第九章 导航功能包集进阶 navigation 书中,大部分出现hydro的地方,直接替换为indigo或jade或kinetic,即可在对应版本中 ...
- SceneKit做一个旋转的地球效果
SceneKit可以用寥寥几行帮你完成很多OpenGL复杂的3D设置代码,下面本猫就带大家完成一个旋转的3D地球的场景. 首先需要地球表面图片,将其导入到Xcode中: 我们用SceneKit内置的几 ...