当前位置:网站首页>Depth analysis of compilation constants, classloader classes, and system class loaders

Depth analysis of compilation constants, classloader classes, and system class loaders

2022-07-07 03:23:00 Watchman under Lighthouse

Compile constants 、ClassLoader class 、 Depth analysis of system class loader

Class initialization

image-20220705211544579

  • Class initialization steps

image-20220705213733318

  • Class initialization time

image-20220705213720887

Example :

/**
 * @name: FinalTest
 * @author: terwer
 * @date: 2022-07-05 21:51
 **/
public class Test2 {
    public static void main(String[] args) {
        System.out.println(FinalTest.x);
    }
}

class FinalTest {
    public static final int x = 6 / 3;

    static {
        System.out.println("FinalTest static block");
    }
}

Running results

image-20220705224430483

/**
 * @name: Test3
 * @author: terwer
 * @date: 2022-07-05 22:24
 **/
public class Test3 {
    public static void main(String[] args) {
        System.out.println(FinalTest2.x);
    }
}

class FinalTest2{
    public static final int x = new Random().nextInt(100);

    static {
        System.out.println("FinalTest2 static block");
    }
}

image-20220705224458495

In front of a x Is a constant , It can be determined when compiling , therefore , Class will not be initialized .

Back one x It cannot be determined during compilation , The operation phase can be determined, so , Class will be initialized .

image-20220705224752721

Example

/**
 * @name: Test4
 * @author: terwer
 * @date: 2022-07-05 22:52
 **/
public class Test4 {
    static {
        System.out.println("Test4 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");
    }
}

Running results

image-20220705230033478

Example

/**
 * @name: Test5
 * @author: terwer
 * @date: 2022-07-05 23:02
 **/
public class Test5 {
    static {
        System.out.println("Test5 static block");
    }

    public static void main(String[] args) {
        Parent2 parent;

        System.out.println("-----------------");

        parent = new Parent2();

        System.out.println(Parent2.a);

        System.out.println(Child2.b);
    }
}

Running results

image-20220705231636865

For subclasses in programs “ Active use ” This will cause the parent class to be initialized , however , Active use of the parent class does not result in subclass initialization ( It is impossible to say that a Object Class objects cause all subclasses in the system to be initialized ).

image-20220705232420092

Example

/**
 * @name: Test6
 * @author: terwer
 * @date: 2022-07-05 23:27
 **/
public class Test6 {
    public static void main(String[] args) {
        System.out.println(Child3.a);

        Child3.dosomething();
    }
}

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");
    }
}

Running results

image-20220705233448532

image-20220705233824150

Example

/**
 * @name: Test7
 * @author: terwer
 * @date: 2022-07-05 23:45
 **/
public class Test7 {
    public static void main(String[] args) throws Exception {
        ClassLoader classLoader = ClassLoader.getSystemClassLoader();
        Class<?> clazz = classLoader.loadClass("com.terwergreen.classloader.C");
        System.out.println("-------------------");

        Class.forName("com.terwergreen.classloader.C");
    }
}

class C {
    static {
        System.out.println("Class C");
    }
}

Running effect

image-20220705235208523

原网站

版权声明
本文为[Watchman under Lighthouse]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/188/202207062004240237.html