zoukankan      html  css  js  c++  java
  • 静态内部类的加载方式

    非内部类的静态成员变量会直接加载

    public class OuterClass  
    {  
        static{  
            System.out.println("OuterClass static load.");  
        }  
      
        public OuterClass()  
        {  
            System.out.println("flag");  
        }  
        public OuterClass(String flag)  
        {  
            System.out.println("flag:"+flag);  
        }  
          
        class InnerClass  
        {  
            //private static String te = "";  
            /*static{ 
                System.out.println("InnerClass static load."); 
            }*/   
            private OuterClass out = new OuterClass("inner");  
        }  
        static class InnerStaticClass  
        {  
            private static OuterClass out = new OuterClass("innerStatic");  
            static{  
                System.out.println("InnerStaticClass static load.");  
            }   
            private static void load()  
            {  
                System.out.println("InnerStaticClass func load().");  
            }  
        }  
        public static  OuterClass getInstatnce()  
       {  
          return OuterClass.InnerStaticClass.out;  
       }  
        public static void main(String[] args)  
        {  
    //        OuterClass.InnerStaticClass.load();  
    //        OuterClass out = OuterClass.InnerStaticClass.out;  
    //        OuterClass.InnerClass innerClass = out.new InnerClass() ;  
            //OuterClass.InnerStaticClass.out.  
        }  
          
    } 

    打印结果:OuterClass static load.
    public class OuterClass  
    {  
        static{  
            System.out.println("OuterClass static load.");  
        }  
      
        public OuterClass()  
        {  
            System.out.println("flag");  
        }  
        public OuterClass(String flag)  
        {  
            System.out.println("flag:"+flag);  
        }  
          
        class InnerClass  
        {  
            //private static String te = "";  
            /*static{ 
                System.out.println("InnerClass static load."); 
            }*/   
            private OuterClass out = new OuterClass("inner");  
        }  
        static class InnerStaticClass  
        {  
            private static OuterClass out = new OuterClass("innerStatic");  
            static{  
                System.out.println("InnerStaticClass static load.");  
            }   
            private static void load()  
            {  
                System.out.println("InnerStaticClass func load().");  
            }  
        }  
        public static  OuterClass getInstatnce()  
       {  
          return OuterClass.InnerStaticClass.out;  
       }  
        public static void main(String[] args)  
        {  
            OuterClass.InnerStaticClass.load();  
    //        OuterClass out = OuterClass.InnerStaticClass.out;  
    //        OuterClass.InnerClass innerClass = out.new InnerClass() ;  
            //OuterClass.InnerStaticClass.out.  
        }  
          
    }  

    内部类只会在第一次使用时加载,不使用就不加载。
    打印结果:

    OuterClass static load.
    flag:innerStatic
    InnerStaticClass static load.
    InnerStaticClass func load().

     

    public class OuterClass  
    {  
        static{  
            System.out.println("OuterClass static load.");  
        }  
      
        public OuterClass()  
        {  
            System.out.println("flag");  
        }  
        public OuterClass(String flag)  
        {  
            System.out.println("flag:"+flag);  
        }  
          
        class InnerClass  
        {  
            //private static String te = "";  
            /*static{ 
                System.out.println("InnerClass static load."); 
            }*/   
            private OuterClass out = new OuterClass("inner");  
        }  
        static class InnerStaticClass  
        {  
            private static OuterClass out = new OuterClass("innerStatic");  
            static{  
                System.out.println("InnerStaticClass static load.");  
            }   
            private static void load()  
            {  
                System.out.println("InnerStaticClass func load().");  
            }  
        }  
        public static  OuterClass getInstatnce()  
       {  
          return OuterClass.InnerStaticClass.out;  
       }  
        public static void main(String[] args)  
        {  
            OuterClass.InnerStaticClass.load();  
            OuterClass out = OuterClass.InnerStaticClass.out;  
            OuterClass.InnerClass innerClass = out.new InnerClass() ;  
        }  
          
    }  
    打印结果:

    OuterClass static load.
    flag:innerStatic
    InnerStaticClass static load.
    InnerStaticClass func load().
    flag:inner

     

  • 相关阅读:
    coredata相关文章,还不错
    Predicates
    iOS5新开发的API总述
    免费Excel教程及模板 微软自带 无木马
    历史的商业行为
    销售激励管理流程和Sales Force Mgmt流程
    亚马逊,应用网关 AWS API GATEWAY
    EXCEL 常用函数公式
    华尔街之狼 原型
    FW:Docker与CI持续集成/CD
  • 原文地址:https://www.cnblogs.com/leihupqrst/p/6812385.html
Copyright © 2011-2022 走看看