12- class loader depth profiling and class initialization

Class loader depth profiling and class initialization

  • Example one:

    • From the operating results of view, FinalTest has not been initialized, x = 3 only at compile into a constant pool of FinalTest!

    • If public static final int x = 3; to:. Public static final int x = new Random () Int (3), block static statements is also output, the FinalTest been initialized.

      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
      
  • Example 2:

    • Nothing wrong with careful analysis! !

      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
      
  • Three examples:

    • Because parent2 = new Parent2 () ;, the parent class has been initialized, so do not say, at initialization Child2 go back when initialize Parent2 and implementation System.out.println ( "Parent2 static block");

      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; //从运行结果来看,这里仅仅是声明对类的引用,并不是对该类的主动使用,所以,不会初始化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
      
  • ★★★ Four examples ★★★

    • The results can be seen from Run: Child_3 Static not print the code block, i.e., Child_3 not been initialized;

    • If by the class name of the subclass access to the parent class's static variable or static method, in essence, is to take the initiative to use the parent class, but not the subclasses of active use.

      class Parent_3{
          static int a = 3 ;
          static{
              System.out.println("Parent_3 static block");
          }
          static void doSomething(){
              System.out.println("do Something");
          }
      }
      class Child_3 extends Parent_3{
          static int b = 4 ;
          static{
              System.out.println("Child_3 static block");
          }
      }
      public class MyTest11 {
          public static void main(String[] args) {
              System.out.println(Child_3.a);
              System.out.println("-------------------------");
              Child_3.doSomething();
          }
      }
      运行结果:
            Parent_3 static block
            3
            -------------------------
            do Something
      
  • Examples of five:

    • loadClass method calls the ClassLoader class loads a class, the class is not actively used, and will not cause class initialization.

      class CL{
          static{
              System.out.println("Class CL");
          }
      }
      public class MyTest12 {
          public static void main(String[] args) throws Exception {
              ClassLoader loader = ClassLoader.getSystemClassLoader();
              Class<?> clazz = loader.loadClass("Jvm.CL");
              System.out.println(clazz);
              System.out.println("-------------------------");
              clazz = Class.forName("Jvm.CL");  //这是反射,这会加载
              System.out.println(clazz);
          }
      }
      运行结果:
         class Jvm.CL
         -------------------------
         Class CL
         class Jvm.CL
      
Published 12 original articles · won praise 0 · Views 219

Guess you like

Origin blog.csdn.net/qq_40574305/article/details/104784486