首页 > 代码库 > 静态内部类的加载方式
静态内部类的加载方式
非内部类的静态成员变量会直接加载
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
静态内部类的加载方式
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。