小编Sri*_*san的帖子

Why static block is not executed

As per java doc, static block is executed when the class is initialized.

Could anyone please tell me why static block is not executed when I run below code?

class A {
    static {
        System.out.println("Static Block");
    }
}

public class Main {

    public static void example1() {
        Class<?> class1 = A.class;
        System.out.println(class1);
    }


    public static void example2() {
        try {
            Class<?> class1 = Class.forName("ClassLoading_Interview_Example.ex1.A");
            System.out.println(class1);
        }catch(Exception e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        example1();
    }
}
Run Code Online (Sandbox Code Playgroud)

java static-block classloading

6
推荐指数
1
解决办法
1316
查看次数

标签 统计

classloading ×1

java ×1

static-block ×1