静态内部类的加载方式
Posted leihupqrst
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了静态内部类的加载方式相关的知识,希望对你有一定的参考价值。
非内部类的静态成员变量会直接加载
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
以上是关于静态内部类的加载方式的主要内容,如果未能解决你的问题,请参考以下文章