责编 | 刘静
出品 | CSDN博客
前言
类加载时机
创建类的实例
-XX:+TraceClassLoading 监控类的加载
在IDE配置如下:
demo代码:
public class ClassLoadInstance {
static {
System.out.println("ClassLoadInstance类初始化时就会被执行!");
}
public ClassLoadInstance() {
System.out.println("ClassLoadInstance构造函数!");
}
}
public class ClassLoadTest {
public static void main(String[] args) {
ClassLoadInstance instance = new ClassLoadInstance();
}
}
运行结果:
结论:
public class ClassLoadStaticVariable {
static {
System.out.println("ClassLoadStaticVariable类初始化时就会被执行!");
}
public static int i = 100;
public ClassLoadStaticVariable() {
System.out.println("ClassLoadStaticVariable构造函数!");
}
}
public class ClassLoadTest {
public static void main(String[] args) {
System.out.println(ClassLoadStaticVariable.i);
}
}
运行结果:
结论:
public class ClassLoadStaticMethod {
static {
System.out.println("ClassLoadStaticMethod类初始化时就会被执行!");
}
public static void method(){
System.out.println("静态方法被调用");
}
public ClassLoadStaticMethod() {
System.out.println("ClassLoadStaticMethod构造函数!");
}
}
public class ClassLoadTest {
public static void main(String[] args) {
ClassLoadStaticMethod.method();
}
}
运行结果:
结论:
package classload;
public class ClassLoadStaticReflect {
static {
System.out.println("ClassLoadStaticReflect类初始化时就会被执行!");
}
public static void method(){
System.out.println("静态方法被调用");
}
public ClassLoadStaticReflect() {
System.out.println("ClassLoadStaticReflect构造函数!");
}
}
public class ClassLoadTest {
public static void main(String[] args) throws ClassNotFoundException {
Class.forName("classload.ClassLoadStaticReflect");
}
}
运行结果:
结论:
反射得到类ClassLoadStaticReflect时,发现ClassLoadStaticReflect类被加载啦,因此反射会触发类加载。
当初始化一个类时,发现其父类还未初始化,则先触发父类的初始化
//父类
public class ClassLoadSuper {
static {
System.out.println("ClassLoadSuper类初始化时就会被执行!这是父类");
}
public static int superNum = 100;
public ClassLoadSuper() {
System.out.println("父类ClassLoadSuper构造函数!");
}
}
//子类
public class ClassLoadSub extends ClassLoadSuper {
static {
System.out.println("ClassLoadSub类初始化时就会被执行!这是子类");
}
public static int subNum = 100;
public ClassLoadSub() {
System.out.println("子类ClassLoadSub构造函数!");
}
}
public class ClassLoadTest {
public static void main(String[] args) throws ClassNotFoundException {
ClassLoadSub classLoadSub = new ClassLoadSub();
}
}
运行结果:
看了运行结果,是不是发现,网上那道经典面试题(讲讲类的实例化顺序?)也很清晰啦。先父类静态变量/静态代码块-> 再子类静态变量/静态代码块->父类构造器->子类构造器
package classload;
public class ClassLoadTest {
public static void main(String[] args) {
System.out.println(ClassLoadSub.subNum);
}
}
运行结果:
结论:
练习与小结
class SingleTon {
private static SingleTon singleTon = new SingleTon();
public static int count1;
public static int count2 = 0;
private SingleTon() {
count1++;
count2++;
}
public static SingleTon getInstance() {
return singleTon;
}
}
public class ClassLoadTest {
public static void main(String[] args) {
SingleTon singleTon = SingleTon.getInstance();
System.out.println("count1=" + singleTon.count1);
System.out.println("count2=" + singleTon.count2);
}
}
分析:
☞【只有光头才能变强,文末有xx】分享一波Lambda表达式
☞从4个维度深度剖析闪电网络现状,在CKB上实现闪电网络的理由 | 博文精选
☞一文了解超级账本DLT、库、开发工具有哪些, Hyperledger家族成员你认识几个?
你点的每个“在看”,我都认真当成了喜欢