码迷,mamicode.com
首页 > 其他好文 > 详细

静态内部类的加载方式

时间:2017-05-05 14:13:50      阅读:161      评论:0      收藏:0      [点我收藏+]

标签:使用   system   div   int   静态   stat   []   成员   变量   

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

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

 

静态内部类的加载方式

标签:使用   system   div   int   静态   stat   []   成员   变量   

原文地址:http://www.cnblogs.com/leihupqrst/p/6812385.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!