非内部类的静态成员变量会直接加载
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