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 MyClassLoaderSample is load by : loader1Dog is load by : loader1Sample is load by : loader3Dog is load by : loader3 |
|
1
2
3
4
5
|
D:\myapp\syslib>java MyClassLoaderSample is load by : sun.misc.Launcher$AppClassLoader@659e0bfdDog is load by : sun.misc.Launcher$AppClassLoader@659e0bfdSample is load by : loader3Dog is load by : loader3 |
|
1
2
3
4
5
|
D:\myapp\syslib>java -cp .;d:\myapp\serverlib MyClassLoaderSample is load by : sun.misc.Launcher$AppClassLoader@659e0bfdDog is load by : sun.misc.Launcher$AppClassLoader@659e0bfdSample is load by : loader3Dog is load by : loader3 |
|
1
2
3
4
5
|
D:\myapp\syslib>java MyClassLoaderSample is load by : loader1Dog is load by : sun.misc.Launcher$AppClassLoader@659e0bfdSample is load by : loader3Dog 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 MyClassLoaderSample is load by : loader1Dog is load by : loader1Exception 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 MyClassLoaderSample is load by : loader1Dog is load by : loader1v1: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 MyClassLoaderobjClass's hashCode is 1311053135Sample is load by : loader1Dog is load by : loader1objClass'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类的生命周期问题有一些疑惑, ...
随机推荐
- springMVC源码分析--HandlerMethodReturnValueHandlerComposite返回值解析器集合(二)
在上一篇博客springMVC源码分析--HandlerMethodReturnValueHandler返回值解析器(一)我们介绍了返回值解析器HandlerMethodReturnValueHand ...
- ajax中xmlhttp.readyState和xmlhttp.status的值及解释
xmlhttp.readyState的值及解释: 0:请求未初始化(还没有调用 open()). 1:请求已经建立,但是还没有发送(还没有调用 send()). 2:请求已发送,正在处理中(通常现在可 ...
- Dynamics CRM 导出系统中实体的属性字段到EXCEL
我们在CRM中看元数据信息,可以通过SDK中的metadata browser的解决方案包,但该解决方案包只是在可视化上方便了,但如果我们需要在excel中整理系统的数据字典时这个解决方案包就派不上用 ...
- Django项目实践4 - Django站点管理(后台管理员)
http://blog.csdn.net/pipisorry/article/details/45079751 上篇:Django项目实践3 - Django模型 Introduction 对于某一类 ...
- python模块collections中namedtuple()的理解
Python中存储系列数据,比较常见的数据类型有list,除此之外,还有tuple数据类型.相比与list,tuple中的元素不可修改,在映射中可以当键使用.tuple元组的item只能通过index ...
- JAVA通过继承Thread来创建线程
创建一个线程的第二种方法是创建一个新的类,该类继承Thread类,然后创建一个该类的实例. 继承类必须重写run()方法,该方法是新线程的入口点.它也必须调用start()方法才能执行. 实例 // ...
- pipeline(管道)设计模式
- Android Multimedia框架总结(十三)CodeC部分之OpenMAX框架初识及接口与适配层实现
转载请把头部出处链接和尾部二维码一起转载,本文出自逆流的鱼yuiop:http://blog.csdn.net/hejjunlin/article/details/52629598 前言:上篇中介绍O ...
- React Native组件只Image
不管在Android还是在ios原生的开发中,图片都是作为控件给出来的,在RN中也有这么一个控件(Image).根据官网的资料,图片分为本地静态图片,网络图片和混合app资源.一下分类介绍来源官网. ...
- CentOS下将php和mysql命令加入到环境变量中的几种方法
Linux CentOS配置LAPM环境时,为了方便,将php和mysql命令加到系统环境命令,下面我们记录几种在linux下将php和mysql加入到环境变量中的方法. 如果在没有添加到环境变量之前 ...