Java误区: 静态代码块,会在类被加载时自动执行?

在调试代码的时候,发现了一个class初始化的问题,就是class的静态代码块或者类变量初始化失败了,导致代码调用静态方法失败。并且该问题是代码运行到这个逻辑的时候才报错的,而不是启动时就报错。我觉得如果这个类能用spring管理,可能会提前发现问题提,我记得支付组也出现过这个问题。第一次见到没有记住,第二次见到一定要记住,并且记录下来:

Java误区: 静态代码块,会在类被加载时自动执行?_第1张图片
首次调用静态方法报错

类细节1.png

类细节2.png

类细节3.png

后来我找了一些博客,发现了一篇不错的文章,请从 2017-07-24 号开始看起:https://blog.csdn.net/jiese1990/article/details/40154329。
我自己打了点代码试了一下

第一段代码:

package myclass;


import java.lang.reflect.Field;
import java.util.Vector;

class MyClass1 {
    static {//静态块
        System.out.println("static block ");
    }
}

class MyClass2 {
    static {//静态块
        System.out.println("class2 static block ");
    }
    public static void print2() {
        System.out.println("class2");
    }
}
public class Hello {
    public static void main(String[] args) throws ClassNotFoundException {
        ClassLoader loader = Thread.currentThread().getContextClassLoader();
        printClassesOfClassLoader(loader);
        System.out.println("-----------------------------------------------------------------------");
        System.out.println("hello word: " + MyClass1.class);
        printClassesOfClassLoader(loader);
        Class.forName("myclass.MyClass1", false, Hello.class.getClassLoader());
        Class.forName("myclass.MyClass1", false, Hello.class.getClassLoader());

        System.out.println("xx");
        Class.forName("myclass.MyClass1", true, Hello.class.getClassLoader());
        System.out.println("xx");
        Class.forName("myclass.MyClass1", false, Hello.class.getClassLoader());
        System.out.println("xx");
        Class.forName("myclass.MyClass1", true, Hello.class.getClassLoader());
        System.out.println("-----------------------------------------------------------------------");
        MyClass2.print2();
    }


    public static void printClassesOfClassLoader(ClassLoader loader) {
        try {
            Field classesF = ClassLoader.class.getDeclaredField("classes");
            classesF.setAccessible(true);
            Vector> classes = (Vector>) classesF.get(loader);
            for (Class c : classes) {
                System.out.println(c);
            }
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
    }
}

输出:

class com.intellij.rt.execution.application.AppMainV2$Agent
class com.intellij.rt.execution.application.AppMainV2
class com.intellij.rt.execution.application.AppMainV2$1
class myclass.Hello
-----------------------------------------------------------------------
hello word: class myclass.MyClass1
class com.intellij.rt.execution.application.AppMainV2$Agent
class com.intellij.rt.execution.application.AppMainV2
class com.intellij.rt.execution.application.AppMainV2$1
class myclass.Hello
class myclass.MyClass1
xx
static block 
xx
xx
-----------------------------------------------------------------------
class2 static block 
class2

第二段代码:

package myclass;

import visitor.AddGraphicVisitor;
import visitor.GraphicVisitor;

import java.lang.reflect.Field;
import java.util.Vector;


public class Main {

    public static void main(String[] args) {
        ClassLoader loader = Thread.currentThread().getContextClassLoader();
        printClassesOfClassLoader(loader);
        GraphicVisitor x = new AddGraphicVisitor();
        System.out.println("-------------------- hello " + MyClass1.class + " --------------------");
        printClassesOfClassLoader(loader);

    }

    public static void printClassesOfClassLoader(ClassLoader loader) {
        try {
            Field classesF = ClassLoader.class.getDeclaredField("classes");
            classesF.setAccessible(true);
            Vector> classes = (Vector>) classesF.get(loader);
            for (Class c : classes) {
                System.out.println(c);
            }
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
    }
}

输出为:

class com.intellij.rt.execution.application.AppMainV2$Agent
class com.intellij.rt.execution.application.AppMainV2
class com.intellij.rt.execution.application.AppMainV2$1
class myclass.Main
-------------------- hello class myclass.MyClass1 --------------------
class com.intellij.rt.execution.application.AppMainV2$Agent
class com.intellij.rt.execution.application.AppMainV2
class com.intellij.rt.execution.application.AppMainV2$1
class myclass.Main
interface visitor.GraphicVisitor
class visitor.AddGraphicVisitor
class myclass.MyClass1

基本思想是:

  1. 类加载和init不一定是一个接着一个执行的,有可能类已经加载了,但是init还没有执行。
  2. 类被用到时才会被加载(如果该类不被spring管理的话)
  3. 执行类的静态方法时,static代码块也会执行。
  4. 创建一个类的实例时,static代码块也会执行。

你可能感兴趣的:(Java误区: 静态代码块,会在类被加载时自动执行?)