初始化对于类与接口的异同点
示例代码
public class MyTest5 {
public static void main(String[] args) {
System.out.println(MyChild5.b);
}
}
interface MyParent5 {
public static Thread thread = new Thread(){
{
System.out.println("MyParent5 invoked");
}
};
}
class MyChild5 implements MyParent5 {
public static int b = 5;
}
输出
5
修改代码如下:
public class MyTest5 {
public static void main(String[] args) {
System.out.println(MyChild5.b);
}
}
class MyParent5 {
public static Thread thread = new Thread(){
{
System.out.println("MyParent5 invoked");
}
};
}
class MyChild5 extends MyParent5 {
public static int b = 5;
}
输出
MyParent5 invoked
5
结论
在初始化一个类时,并不会先初始化它所实现的接口
示例
public class MyTest5 {
public static void main(String[] args) {
System.out.println(MyParent5.thread);
}
}
interface MyGrandpa{
public static Thread thread = new Thread(){
{
System.out.println("MyGrandpa invoked");
}
};
}
interface MyParent5 extends MyGrandpa{
public static Thread thread = new Thread(){
{
System.out.println("MyParent5 invoked");
}
};
}
输出
MyParent5 invoked
Thread[Thread-0,5,main]
结论
在初始化一个接口时,并不会先初始化它的父接口。
类加载器双亲委托机制
- 在父亲委托机制中,各个加载器按照父子关系形成了树形结构,除了根类加载器之外,其余的类加载器都有且只有一个父加载器。
Bootstrap ClassLoader /启动类加载器
$JAVA_HOME中jre/lib/rt.jar里面所有的class,由C++实现,不是ClassLoader子类。
Extension ClassLoader /扩展类加载器
负责加载java平台中扩展功能的一些jar包,包括$JAVA_HOME中jre/lib/*.jar或-Djava.ext.dirs指定目录下的jar包。
App ClassLoader /系统类加载器
负责加载classpath中指定的jar包及目录中class
若有一个类加载器能够成功加载Test类,那么这个类加载器被称为定义类加载器,所有能成功返回Class对象引用的类加载器(包括定义类加载器)都被称为初始类加载器。
示例
package com.leofight.jvm.classloader;
public class MyTest7 {
public static void main(String[] args) throws Exception {
Class<?> clazz = Class.forName("java.lang.String");
System.out.println(clazz.getClassLoader());
Class<?> clazz2 = Class.forName("com.leofight.jvm.classloader.C");
System.out.println(clazz2.getClassLoader());
}
}
class C {
}
输出
null
sun.misc.Launcher$AppClassLoader@14dad5dc
类加载器与类初始化示例
示例1
package com.leofight.jvm.classloader;
import java.util.Random;
class FinalTest {
public static final int x = 3;
static {
System.out.println("FinalTest static block");
}
}
public class MyTest8 {
public static void main(String[] args) {
System.out.println(FinalTest.x);
}
}
输出
3
示例2
package com.leofight.jvm.classloader;
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");
}
}
public class MyTest9 {
static {
System.out.println("MyTest9 static block");
}
public static void main(String[] args) {
System.out.println(Child.b);
}
}
输出
MyTest9 static block
Parent static block
Child static block
4
类加载情况
[Loaded com.leofight.jvm.classloader.MyTest9 from file:/Users/lizhi/IdeaProjects/jvm_lecture/out/production/classes/]
[Loaded com.leofight.jvm.classloader.Parent from file:/Users/lizhi/IdeaProjects/jvm_lecture/out/production/classes/]
[Loaded com.leofight.jvm.classloader.Child from file:/Users/lizhi/IdeaProjects/jvm_lecture/out/production/classes/]
示例3
package com.leofight.jvm.classloader;
class Parent2 {
static int a = 3;
static {
System.out.println("Parent2 static block");
}
}
class Child2 extends Parent2 {
static int b = 4;
static {
System.out.println("Child2 static block");
}
}
public class MyTest10 {
static {
System.out.println("MyTest10 static block");
}
public static void main(String[] args) {
Parent2 parent2;
System.out.println("-----------");
parent2 = new Parent2();
System.out.println("-----------");
System.out.println(parent2.a);
System.out.println("------------");
System.out.println(Child2.b);
}
}
输出
MyTest10 static block
-----------
Parent2 static block
-----------
3
------------
Child2 static block
4
示例4
package com.leofight.jvm.classloader;
class Parent3 {
static int a = 3;
static {
System.out.println("Parent3 static block");
}
static void doSomething() {
System.out.println("do something");
}
}
class Child3 extends Parent3 {
static {
System.out.println("Child3 static block");
}
}
public class MyTest11 {
public static void main(String[] args) {
System.out.println(Child3.a);
Child3.doSomething();
}
}
输出
Parent3 static block
3
do something
示例5
package com.leofight.jvm.classloader;
class CL {
static {
System.out.println("Class CL");
}
}
public class MyTest12 {
public static void main(String[] args) throws Exception {
ClassLoader classLoader = ClassLoader.getSystemClassLoader();
Class<?> clazz = classLoader.loadClass("com.leofight.jvm.classloader.CL");
System.out.println(clazz);
System.out.println("---------");
clazz = Class.forName("com.leofight.jvm.classloader.CL");
System.out.println(clazz);
}
}
输出
class com.leofight.jvm.classloader.CL
---------
Class CL
class com.leofight.jvm.classloader.CL
调用ClassLoader类的loadClass方法加载一个类,并不是对类的主动使用,不会导致类的初始化。
不同的类加载器作用与加载动作分析
示例代码1
package com.leofight.jvm.classloader;
public class MyTest13 {
public static void main(String[] args) {
ClassLoader classLoader = ClassLoader.getSystemClassLoader();
System.out.println(classLoader);
while (null != classLoader) {
classLoader = classLoader.getParent();
System.out.println(classLoader);
}
}
}
输出
sun.misc.Launcher$AppClassLoader@14dad5dc
sun.misc.Launcher$ExtClassLoader@61bbe9ba
null
示例代码2
package com.leofight.jvm.classloader;
import java.net.URL;
import java.util.Enumeration;
public class MyTest14 {
public static void main(String[] args) throws Exception {
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
String resourceName = "com/leofight/jvm/classloader/MyTest13.class";
Enumeration<URL> urls = classLoader.getResources(resourceName);
while (urls.hasMoreElements()) {
URL url = urls.nextElement();
System.out.println(url);
}
System.out.println("----------");
Class<?> clazz = MyTest14.class;
System.out.println(clazz.getClassLoader());
}
}
输出
file:/Users/lz/IdeaProjects/jvm_lecture/out/production/classes/com/leofight/jvm/classloader/MyTest13.class
获取当前类的ClassLoader
clazz.getClassLoader();
获取当前线程上下文的ClassLoader
Thread.currentThread().getContextClassLoader();
获取系统的ClassLoader
ClassLoader.getSystemClassLoader();
获取调用者的ClassLoader
DriverManager.getCallerClassLoader();