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类的生命周期问题有一些疑惑, ...
随机推荐
- PHP HTTP 函数
PHP HTTP 简介 HTTP 函数允许您在其他输出被发送之前,对由 Web 服务器发送到浏览器的信息进行操作. 安装 HTTP 函数是 PHP 核心的组成部分.无需安装即可使用这些函数. PHP ...
- JUnit单元测试教程(翻译自Java Code Geeks)
JUnit单元测试教程--终极指南 JUnit单元测试教程终极指南 说明 单元测试简介 1 什么是单元测试 2 测试覆盖 3 Java中的单元测试 JUnit简介 1 使用Eclipse实现简单JUn ...
- xlwt以格式生成xls文件
参考: http://blog.sina.com.cn/s/blog_5357c0af01019gjo.html http://www.programcreek.com/python/example/ ...
- Bootstrap3 代码-内联代码
通过 <code> 标签包裹内联样式的代码片段. For example, <section> should be wrapped as inline. For example ...
- 潜谈IT从业人员在传统IT和互联网之间的择业问题(下)-互联网公司
互联网带来的一片晴天 相对于传统行业来说,互联网行业要显得相对对技术人员尊重些. 在互联网行业中,采用的技术.概念也较传统形行业来说要新,技术人员也容易在此找到自己的一方净土. 因为互联网这个行当讲究 ...
- Android简易实战教程--第三十五话《音乐播放》
已经好几天不更新博客了,今天轻松一点模拟个简单的"音乐播放器".1分钟看完~ 整个简单布局,加几个控制按钮: <LinearLayout xmlns:android=&quo ...
- solr界面
1.1 界面功能介绍 1.1.1 Analysis
- Android基础知识点-Manifest清单文件
每个应用的根目录中都必须包含一个 AndroidManifest.xml 文件(且文件名精确无误). 清单文件向 Android 系统提供应用的必要信息,系统必须具有这些信息方可运行应用的任何代码. ...
- Markdown对应Yelee主题语法
概述 这里说的是Yelee主题的语法和原生语法是有些区别的:更多的基础语法可以到Cmd Markdown上面去查看:但是我觉得都会各有不同吧 注意这里说的不是真正意义上的Markdown语法 标题 一 ...
- iOS 用RunTime来提升按钮的体验
用RunTime来提升按钮的体验 载请标明出处:http://blog.csdn.net/sk719887916/article/details/52597388,作者:Ryan 经常处理按钮问题都是 ...