12 - クラスローダの深さプロファイリングとクラスの初期化

クラスローダの深さプロファイリングとクラスの初期化

  • 例1:

    • ビューの運転結果から、FinalTestはFinalTestの定数プールにのみ、コンパイル時のx = 3、初期設定されていません!

    • 場合のpublic static final int型のx = 3;も出力さに:.のpublic static final int型のx =新しいランダム()のInt(3)、ブロック静的文、FinalTestが初期化されて。

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

    • 慎重な分析と何も間違っています!

      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
      
  • 三つの例:

    • 、親クラスが初期化されている、そう言っていない、Parent2および実装するSystem.out.println(「Parent2の静的ブロック」)を初期化するときCHILD2初期化時に戻って、parent2 =新しいParent2()からです。

      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
      
  • ★★★ 四例 ★★★

    • 結果は、ランから分かる:Child_3静的コードブロックを出力しない、すなわち、Child_3初期化されていません。

    • よるならばサブクラスのクラス名、親クラスの静的変数または静的メソッドへのアクセス、本質的には、親クラスを使用するためのイニシアチブを取ることですが、アクティブな使用のないサブクラス。

      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
      
  • 5の例:

    • loadClassメソッドは、クラスがアクティブに使用されていない、クラスClassLoaderクラスのロードを呼び出し、しませんクラスの初期化の原因。

      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
      
公開された12元の記事 ウォンの賞賛0 ビュー219

おすすめ

転載: blog.csdn.net/qq_40574305/article/details/104784486