内部类---静态内部类和非静态内部类

时间:2021-12-28 05:38:50

package com.dong.test;


/**
 * 外部类
 *
 */
public class OutClass {
private static String name;


static {
System.out.println("static OutClass");
}


public OutClass() {
System.out.println("c OutClass");
}


/**
* 非静态内部类
*
*/
public class InClass1 {
private String age1;


{
age1 = "";
System.out.println("unstatic InClass1");
}


public InClass1() {
System.out.println("c InClass1");
}
}


/**
* 静态内部类
*
*/
public static class InClass {
private static String age;


static {
System.out.println("static InClass");
}


public InClass() {
System.out.println("c InClass");
}
}


public static void say() {
// System.out.println("name:" + name);
System.out.println("name:" + name + " age:" + InClass.age);//此行代码才会初始化InClass
}


public void say1() {
// System.out.println("name:" + name);
  System.out.println("name:" + name + " age1:" + new InClass1().age1);//此行代码才会初始化InClass1
}


}



package com.dong.test;


public class Test {

public static void main(String[] args)throws Exception{

// Class.forName("com.dong.test.OutClass");
OutClass out = new OutClass();
// out.say1();


OutClass.InClass1 in1 = out.new InClass1();

//  OutClass.InClass in = new OutClass.InClass();


}


}