java中可以使用static关键字来声明一个成员内部类,该类被称为静态内部类,它可以在不创建外部类的对象的情况下被实例化。
外部类名.内部类名 变量名=new 外部类名.内部类名();
演示
外部类
class Outer
{
private static int num=6;
static class Inner
{
void show()
{
System.out.println("num="+num);
}
}
}
class Example18
{
public static void main(String []args)
{
Outer.Inner inner =new Outer.Inner();
inner.show();
}
}
1.静态内部类中只能访问外部静态类的成员 ,如果去掉static 则程序报错
2.在静态内部类中可以定义静态的成员,而在非静态内部类中不能定义静态的成员